-
-
Notifications
You must be signed in to change notification settings - Fork 995
fix: add retry logic with exponential backoff to hook fetch calls #530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -56,6 +56,68 @@ export function clearPortCache(): void { | |||||
| cachedHost = null; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Fetch with retry logic for transient network errors. | ||||||
| * Handles ECONNRESET, ECONNREFUSED, ETIMEDOUT, and socket errors. | ||||||
| * | ||||||
| * With default maxRetries=3, performs up to 4 total attempts: | ||||||
| * - Initial attempt | ||||||
| * - Up to 3 retries with exponential backoff (100ms, 200ms, 400ms) | ||||||
| */ | ||||||
| export async function fetchWithRetry( | ||||||
| url: string, | ||||||
| options: RequestInit, | ||||||
| config: { | ||||||
| maxRetries?: number; | ||||||
| initialDelayMs?: number; | ||||||
| maxDelayMs?: number; | ||||||
| } = {} | ||||||
| ): Promise<Response> { | ||||||
| const { | ||||||
| maxRetries = 3, | ||||||
| initialDelayMs = 100, | ||||||
| maxDelayMs = 1000 | ||||||
| } = config; | ||||||
|
Comment on lines
+76
to
+80
|
||||||
|
|
||||||
| const retryableErrors = ['ECONNRESET', 'ECONNREFUSED', 'ETIMEDOUT', 'UND_ERR_SOCKET']; | ||||||
|
|
||||||
| for (let attempt = 0; attempt <= maxRetries; attempt++) { | ||||||
|
||||||
| try { | ||||||
| return await fetch(url, options); | ||||||
| } catch (error) { | ||||||
| const errorCode = (error as any)?.cause?.code || ''; | ||||||
| const errorMessage = (error as Error).message || ''; | ||||||
|
|
||||||
| const isRetryable = retryableErrors.some(code => | ||||||
| errorCode.includes(code) || errorMessage.includes(code) | ||||||
|
||||||
| errorCode.includes(code) || errorMessage.includes(code) | |
| errorCode === code || errorMessage.includes(code) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation comment states "performs up to 4 total attempts" but this is misleading. With maxRetries=3, the loop runs from attempt=0 to attempt=3 (inclusive), which is indeed 4 attempts total. However, the comment then lists "Up to 3 retries with exponential backoff (100ms, 200ms, 400ms)" which only shows 3 delay values.
The actual delays for each retry attempt are:
The comment should clarify that the initial attempt has no delay, and the delays shown are for the retry attempts only.