-
Notifications
You must be signed in to change notification settings - Fork 307
add retry for http retryable requests #1390
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,34 +2,89 @@ import type * as http from 'node:http' | |
| import type * as https from 'node:https' | ||
| import type * as stream from 'node:stream' | ||
| import { pipeline } from 'node:stream' | ||
| import { promisify } from 'node:util' | ||
|
|
||
| import type { Transport } from './type.ts' | ||
|
|
||
| const pipelineAsync = promisify(pipeline) | ||
|
|
||
| export async function request( | ||
| transport: Transport, | ||
| opt: https.RequestOptions, | ||
| body: Buffer | string | stream.Readable | null = null, | ||
| ): Promise<http.IncomingMessage> { | ||
| return new Promise<http.IncomingMessage>((resolve, reject) => { | ||
| const requestObj = transport.request(opt, (resp) => { | ||
| resolve(resp) | ||
| const requestObj = transport.request(opt, (response) => { | ||
| resolve(response) | ||
| }) | ||
|
|
||
| if (!body || Buffer.isBuffer(body) || typeof body === 'string') { | ||
| requestObj | ||
| .on('error', (e: unknown) => { | ||
| reject(e) | ||
| }) | ||
| .end(body) | ||
| requestObj.on('error', reject) | ||
|
|
||
| return | ||
| if (!body || Buffer.isBuffer(body) || typeof body === 'string') { | ||
| requestObj.end(body) | ||
| } else { | ||
| pipelineAsync(body, requestObj).catch(reject) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| const MAX_RETRIES = 10 | ||
| const EXP_BACK_OFF_BASE_DELAY = 1000 // Base delay for exponential backoff | ||
| const ADDITIONAL_DELAY_FACTOR = 1.0 // to avoid synchronized retries | ||
|
|
||
| // Retryable error codes for HTTP ( ref: minio-go) | ||
| export const retryHttpCodes: Record<string, boolean> = { | ||
| 408: true, | ||
| 429: true, | ||
| 499: true, | ||
| 500: true, | ||
| 502: true, | ||
| 503: true, | ||
| 504: true, | ||
| 520: true, | ||
| } | ||
|
|
||
| const isHttpRetryable = (httpResCode: number) => { | ||
| return retryHttpCodes[httpResCode] !== undefined | ||
| } | ||
|
|
||
| const sleep = (ms: number) => { | ||
| return new Promise((resolve) => setTimeout(resolve, ms)) | ||
| } | ||
|
|
||
| // pump readable stream | ||
| pipeline(body, requestObj, (err) => { | ||
| if (err) { | ||
| reject(err) | ||
| const getExpBackOffDelay = (retryCount: number) => { | ||
| const backOffBy = EXP_BACK_OFF_BASE_DELAY * 2 ** retryCount | ||
| const additionalDelay = Math.random() * backOffBy * ADDITIONAL_DELAY_FACTOR | ||
| return backOffBy + additionalDelay | ||
| } | ||
|
|
||
| export async function requestWithRetry( | ||
| transport: Transport, | ||
| opt: https.RequestOptions, | ||
| body: Buffer | string | stream.Readable | null = null, | ||
| maxRetries: number = MAX_RETRIES, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This option is never respected and never called. |
||
| ): Promise<http.IncomingMessage> { | ||
| let attempt = 0 | ||
| while (attempt <= maxRetries) { | ||
| try { | ||
| const response = await request(transport, opt, body) | ||
| // Check if the HTTP status code is retryable | ||
| if (isHttpRetryable(response.statusCode as number)) { | ||
| throw new Error(`Retryable HTTP status: ${response.statusCode}`) // trigger retry attempt with calculated delay | ||
| } | ||
| }) | ||
| }) | ||
| return response // Success, return the raw response | ||
| } catch (err) { | ||
| attempt++ | ||
|
|
||
| if (attempt > maxRetries) { | ||
| throw new Error(`Request failed after ${maxRetries} retries: ${err}`) | ||
| } | ||
| const delay = getExpBackOffDelay(attempt) | ||
| // eslint-disable-next-line no-console | ||
| // console.warn( `${new Date().toLocaleString()} Retrying request (attempt ${attempt}/${maxRetries}) after ${delay}ms due to: ${err}`,) | ||
| await sleep(delay) | ||
| } | ||
| } | ||
|
|
||
| throw new Error(`${MAX_RETRIES} Retries exhausted, request failed.`) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Base delay of 1 second is not a good value. Based on
getExpBackoffDelayfunction, this will have a maximum value around 1009556 milliseconds, which is 16 minutes.