Skip to content

fix(integration-discord): handle 429 rate limits#334

Merged
gvieira18 merged 2 commits into
4.xfrom
fix/purge-invites-rate-limit
Jun 17, 2026
Merged

fix(integration-discord): handle 429 rate limits#334
gvieira18 merged 2 commits into
4.xfrom
fix/purge-invites-rate-limit

Conversation

@gvieira18

Copy link
Copy Markdown
Member

Summary

  • Add retry logic with jittered backoff (3-6s) to purge-invites command, respecting Discord's retry_after header on 429 responses
  • Increase base delay between delete requests from 200-500ms to 3-6s with jitter to prevent Cloudflare IP-level blocking (error 1015)
  • Extract PurgeInvitesResultDTO and MatchedInviteDTO from raw array returns, with fromDiscordApi(), fromDryRun(), and fromPurge() factory methods

Test plan

  • Existing 7 tests updated and passing
  • New test: retries on 429 and succeeds after backoff
  • New test: fails gracefully after exhausting 3 retries
  • PHPStan, Pint, Rector all clean

Add retry logic with jittered backoff (3-6s) to
purge-invites, preventing Cloudflare IP bans.
Extract DTOs for purge result and matched invite.
@gvieira18 gvieira18 requested a review from a team June 17, 2026 22:11
@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 4c930188-87cc-4c34-bb17-a42861ef21e9

📥 Commits

Reviewing files that changed from the base of the PR and between 5e94192 and b1ff5c9.

📒 Files selected for processing (1)
  • app-modules/integration-discord/src/Sync/Actions/PurgeUnusedInvitesAction.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • app-modules/integration-discord/src/Sync/Actions/PurgeUnusedInvitesAction.php

📝 Walkthrough

Walkthrough

PurgeUnusedInvitesAction::execute return type changes from array to PurgeInvitesResultDTO. Two new readonly DTOs are introduced: MatchedInviteDTO (with fromDiscordApi factory) and PurgeInvitesResultDTO (with fromDryRun/fromPurge factories). Deletion logic is extracted into a deleteInvite private method with a MAX_RETRIES retry loop that reads retry_after from HTTP 429 responses and applies jittered sleeping. PurgeUnusedInvitesCommand is updated to use object property access on the result. Unit tests are updated to assert against object properties and two new tests cover 429 retry success and retry exhaustion.

Possibly related PRs

  • he4rt/heartdevs.com#333: Introduces the original discord:purge-invites command and PurgeUnusedInvitesAction with array-based results that this PR directly refactors into DTOs with retry handling.

Suggested reviewers

  • Clintonrocha98
  • 1pride
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed Title accurately reflects the main change: adding rate limit (429) handling with retry logic to the purge-invites command.
Description check ✅ Passed Description comprehensively covers the rate limit handling, delay adjustments, DTO extraction, and testing approach, all directly relevant to the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@app-modules/integration-discord/src/Sync/Actions/PurgeUnusedInvitesAction.php`:
- Around line 64-67: The array_filter function preserves original keys, which
means the first remaining invite in the $unused array may have an index greater
than 0, causing the condition at line 65 ($index > 0) to trigger unnecessarily
on the first invite and cause an unwanted sleep. Before iterating over $unused
in the foreach loop within PurgeUnusedInvitesAction, apply array_values() to
reindex the filtered invites array with consecutive numeric keys starting from
0, ensuring that only invites after the first one will have $index > 0 and
trigger the jitteredSleep().
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 85fb8d48-9423-4237-b969-94b5f3c76466

📥 Commits

Reviewing files that changed from the base of the PR and between 54cd9e3 and 5e94192.

📒 Files selected for processing (5)
  • app-modules/integration-discord/src/Sync/Actions/PurgeUnusedInvitesAction.php
  • app-modules/integration-discord/src/Sync/Console/PurgeUnusedInvitesCommand.php
  • app-modules/integration-discord/src/Sync/DTOs/MatchedInviteDTO.php
  • app-modules/integration-discord/src/Sync/DTOs/PurgeInvitesResultDTO.php
  • app-modules/integration-discord/tests/Unit/Sync/PurgeUnusedInvitesActionTest.php

Comment thread app-modules/integration-discord/src/Sync/Actions/PurgeUnusedInvitesAction.php Outdated
array_filter preserves keys, causing a spurious
sleep before the first delete when index > 0.
@gvieira18 gvieira18 merged commit f173c39 into 4.x Jun 17, 2026
6 checks passed
@gvieira18 gvieira18 deleted the fix/purge-invites-rate-limit branch June 17, 2026 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants