-
Notifications
You must be signed in to change notification settings - Fork 70
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
Add retryCodes option in HttpMiddlewareOptions #1764
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1cadbff
- add retryCodes option to HttpMiddlewareOptions
ajimae b7c4a59
feat(http-error-retryCodes): improvement
ajimae 0aa84a7
feat(http-error-retryCodes): improvements
ajimae 3205631
chore(retryConfig): retryCodes
ajimae 8f3a895
chore(retryCodes): make '503' a default retryCode
ajimae 04233f4
Merge branch 'master' into feat/add-retry-codes
ajimae 17b36c5
Merge branch 'master' into feat/add-retry-codes
ajimae 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 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 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 |
---|---|---|
|
@@ -35,9 +35,9 @@ function calcDelayDuration( | |
if (backoff) | ||
return retryCount !== 0 // do not increase if it's the first retry | ||
? Math.min( | ||
Math.round((Math.random() + 1) * retryDelay * 2 ** retryCount), | ||
maxDelay | ||
) | ||
Math.round((Math.random() + 1) * retryDelay * 2 ** retryCount), | ||
maxDelay | ||
) | ||
: retryDelay | ||
return retryDelay | ||
} | ||
|
@@ -59,6 +59,7 @@ export default function createHttpMiddleware({ | |
maskSensitiveHeaderData = true, | ||
enableRetry, | ||
timeout, | ||
retryCodes = [], | ||
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.
|
||
retryConfig: { | ||
// encourage exponential backoff to prevent spamming the server if down | ||
maxRetries = 10, | ||
|
@@ -94,6 +95,12 @@ export default function createHttpMiddleware({ | |
fetchFunction = fetch | ||
} | ||
|
||
if (!Array.isArray(retryCodes)) { | ||
throw new Error( | ||
'`retryCodes` option must be an array of retry status (error) codes.' | ||
) | ||
} | ||
|
||
return (next: Next): Next => | ||
(request: MiddlewareRequest, response: MiddlewareResponse) => { | ||
let abortController: any | ||
|
@@ -114,9 +121,9 @@ export default function createHttpMiddleware({ | |
// Ensure body is a string if content type is application/json | ||
const body = | ||
requestHeader['Content-Type'] === 'application/json' && | ||
typeof request.body !== 'string' | ||
typeof request.body !== 'string' | ||
? // NOTE: `stringify` of `null` gives the String('null') | ||
JSON.stringify(request.body || undefined) | ||
JSON.stringify(request.body || undefined) | ||
: request.body | ||
|
||
if (body && (typeof body === 'string' || Buffer.isBuffer(body))) { | ||
|
@@ -132,15 +139,16 @@ export default function createHttpMiddleware({ | |
} | ||
|
||
if (!retryOnAbort) { | ||
if (timeout || getAbortController || _abortController) | ||
if (timeout || getAbortController || _abortController) { | ||
// eslint-disable-next-line | ||
abortController = | ||
abortController = | ||
(getAbortController ? getAbortController() : null) || | ||
_abortController || | ||
new AbortController() | ||
|
||
if (abortController) { | ||
fetchOptions.signal = abortController.signal | ||
if (abortController) { | ||
fetchOptions.signal = abortController.signal | ||
} | ||
} | ||
} | ||
|
||
|
@@ -151,16 +159,18 @@ export default function createHttpMiddleware({ | |
// wrap in a fn so we can retry if error occur | ||
function executeFetch() { | ||
if (retryOnAbort) { | ||
if (timeout || getAbortController || _abortController) | ||
if (timeout || getAbortController || _abortController) { | ||
// eslint-disable-next-line | ||
abortController = | ||
abortController = | ||
(getAbortController ? getAbortController() : null) || | ||
_abortController || | ||
new AbortController() | ||
|
||
if (abortController) { | ||
fetchOptions.signal = abortController.signal | ||
if (abortController) { | ||
fetchOptions.signal = abortController.signal | ||
} | ||
} | ||
|
||
} | ||
// Kick off timer for abortController directly before fetch. | ||
let timer | ||
|
@@ -225,21 +235,6 @@ export default function createHttpMiddleware({ | |
}) | ||
return | ||
} | ||
if (res.status === 503 && enableRetry) | ||
if (retryCount < maxRetries) { | ||
setTimeout( | ||
executeFetch, | ||
calcDelayDuration( | ||
retryCount, | ||
retryDelay, | ||
maxRetries, | ||
backoff, | ||
maxDelay | ||
) | ||
) | ||
retryCount += 1 | ||
return | ||
} | ||
|
||
// Server responded with an error. Try to parse it as JSON, then | ||
// return a proper error type with all necessary meta information. | ||
|
@@ -261,6 +256,28 @@ export default function createHttpMiddleware({ | |
? { message: parsed.message, body: parsed } | ||
: { message: parsed, body: parsed }), | ||
}) | ||
|
||
if ( | ||
enableRetry && | ||
([503, ...retryCodes].indexOf(error.statusCode) !== -1 || | ||
retryCodes?.indexOf(error.message) !== -1) | ||
) { | ||
if (retryCount < maxRetries) { | ||
setTimeout( | ||
executeFetch, | ||
calcDelayDuration( | ||
retryCount, | ||
retryDelay, | ||
maxRetries, | ||
backoff, | ||
maxDelay | ||
) | ||
) | ||
retryCount += 1 | ||
return | ||
} | ||
} | ||
|
||
maskAuthData(error.originalRequest, maskSensitiveHeaderData) | ||
// Let the final resolver to reject the promise | ||
const parsedResponse = { | ||
|
This file contains 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 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
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.
I would have made the retryCodes a property of the retryConfig. And use a default value array with the 503 status code like:
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.
Why I added the 503 in the
if()
statement is because the default 503 will be overridden by the option passed in by the customer, which might not contain the 503 status 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.
But on the other side if you see there is a default of 503 and you don't pass it. I would call it bad luck :)