-
Notifications
You must be signed in to change notification settings - Fork 241
Add automatic retry policy #115
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
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 |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| const ApiError = require('./error'); | ||
|
|
||
| /** | ||
| * Automatically retry a request if it fails with an appropriate status code. | ||
| * | ||
| * A GET request is retried if it fails with a 429 or 5xx status code. | ||
| * A non-GET request is retried only if it fails with a 429 status code. | ||
| * | ||
| * If the response sets a Retry-After header, | ||
| * the request is retried after the number of seconds specified in the header. | ||
| * Otherwise, the request is retried after the specified interval, | ||
| * with exponential backoff and jitter. | ||
| * | ||
| * @param {Function} request - A function that returns a Promise that resolves with a Response object | ||
| * @param {object} options | ||
| * @param {Function} [options.shouldRetry] - A function that returns true if the request should be retried | ||
| * @param {number} [options.maxRetries] - Maximum number of retries. Defaults to 5 | ||
| * @param {number} [options.interval] - Interval between retries in milliseconds. Defaults to 500 | ||
| * @returns {Promise<Response>} - Resolves with the response object | ||
| * @throws {ApiError} If the request failed | ||
| */ | ||
| async function withAutomaticRetries(request, options = {}) { | ||
| const shouldRetry = options.shouldRetry || (() => (false)); | ||
| const maxRetries = options.maxRetries || 5; | ||
| const interval = options.interval || 500; | ||
| const jitter = options.jitter || 100; | ||
|
|
||
| // eslint-disable-next-line no-promise-executor-return | ||
| const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
|
||
| let attempts = 0; | ||
| do { | ||
| let delay = (interval * (2 ** attempts)) + (Math.random() * jitter); | ||
|
|
||
| /* eslint-disable no-await-in-loop */ | ||
| try { | ||
| const response = await request(); | ||
| if (response.ok || !shouldRetry(response)) { | ||
| return response; | ||
| } | ||
| } catch (error) { | ||
| if (error instanceof ApiError) { | ||
| const retryAfter = error.response.headers.get('Retry-After'); | ||
| if (retryAfter) { | ||
| if (!Number.isInteger(retryAfter)) { // Retry-After is a date | ||
| const date = new Date(retryAfter); | ||
| if (!Number.isNaN(date.getTime())) { | ||
| delay = date.getTime() - new Date().getTime(); | ||
| } | ||
| } else { // Retry-After is a number of seconds | ||
| delay = retryAfter * 1000; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (Number.isInteger(maxRetries) && maxRetries > 0) { | ||
| if (Number.isInteger(delay) && delay > 0) { | ||
| await sleep(interval * 2 ** (options.maxRetries - maxRetries)); | ||
| } | ||
| attempts += 1; | ||
| } | ||
| /* eslint-enable no-await-in-loop */ | ||
| } while (attempts < maxRetries); | ||
|
|
||
| return request(); | ||
| } | ||
|
|
||
| module.exports = { withAutomaticRetries }; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.