fix(worker): handle 5xx errors in http retry logic#855
Conversation
This comment has been minimized.
This comment has been minimized.
WalkthroughThis PR introduces automatic retry logic for 5xx HTTP errors in backend utilities. It adds an Octokit error type guard, implements header-based rate-limit handling via the Changes
Sequence DiagramsequenceDiagram
participant Caller
participant fetchWithRetry
participant HTTP as HTTP Request
participant RateLimitCheck as Rate Limit<br/>Check
participant BackoffTimer as Backoff<br/>Timer
Caller->>fetchWithRetry: fetchWithRetry(url)
loop Until success or max attempts
fetchWithRetry->>HTTP: Make request
HTTP-->>fetchWithRetry: Response with status
alt Status is retriable (5xx, 403, 429)
fetchWithRetry->>RateLimitCheck: Check x-ratelimit-reset header
alt Header exists
RateLimitCheck-->>BackoffTimer: Wait until reset time
else
RateLimitCheck-->>BackoffTimer: Exponential backoff
end
BackoffTimer-->>fetchWithRetry: Wait complete
fetchWithRetry->>fetchWithRetry: Increment attempts
else Status not retriable or max attempts reached
fetchWithRetry-->>Caller: Return response or error
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
1 similar comment
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/backend/src/utils.ts (1)
75-98:⚠️ Potential issue | 🟡 MinorLog message is misleading for 5xx errors.
The warning message states "Rate limit exceeded" but the function now retries on 5xx server errors as well, which are not rate limit issues. Consider a more accurate message.
📝 Proposed fix
- logger.warn(`Rate limit exceeded for ${identifier}. Waiting ${waitTime}ms before retry ${attempts}/${maxAttempts}...`); + logger.warn(`Retriable error (status ${e.status}) for ${identifier}. Waiting ${waitTime}ms before retry ${attempts}/${maxAttempts}...`);
🧹 Nitpick comments (1)
packages/backend/src/github.ts (1)
53-65: Useinstanceof RequestErrorinstead of duck typing for more robust type narrowing.The
RequestErrorclass setsnameto"HttpError", so the current duck-type check works for actual Octokit errors. However, the Octokit documentation recommends usinginstanceof RequestErrorfor type narrowing—it's more explicit and avoids potential false positives from non-Octokit errors with matching shape. SinceRequestErroris already imported, refactor to:export const isOctokitRequestError = (error: unknown): error is RequestError => { return error instanceof RequestError; };
Summary by CodeRabbit
New Features
Tests