Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Don't break the default behavior when using custom retry function (#830)
- Loading branch information
1 parent
8eaef94
commit b15ce1d
Showing
10 changed files
with
94 additions
and
68 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import is from '@sindresorhus/is'; | ||
import {HTTPError, ParseError, MaxRedirectsError, GotError} from './errors'; | ||
import { | ||
RetryFunction, | ||
ErrorCode, | ||
StatusCode, | ||
Method | ||
} from './utils/types'; | ||
|
||
const retryAfterStatusCodes: ReadonlySet<StatusCode> = new Set([413, 429, 503]); | ||
|
||
const calculateRetryDelay: RetryFunction = ({attemptCount, retryOptions, error}) => { | ||
if (attemptCount > retryOptions.limit) { | ||
return 0; | ||
} | ||
|
||
const hasMethod = retryOptions.methods.has((error as GotError).options.method as Method); | ||
const hasErrorCode = Reflect.has(error, 'code') && retryOptions.errorCodes.has((error as GotError).code as ErrorCode); | ||
const hasStatusCode = Reflect.has(error, 'response') && retryOptions.statusCodes.has((error as HTTPError | ParseError | MaxRedirectsError).response.statusCode as StatusCode); | ||
if (!hasMethod || (!hasErrorCode && !hasStatusCode)) { | ||
return 0; | ||
} | ||
|
||
const {response} = error as HTTPError | ParseError | MaxRedirectsError; | ||
if (response && Reflect.has(response.headers, 'retry-after') && retryAfterStatusCodes.has(response.statusCode as StatusCode)) { | ||
let after = Number(response.headers['retry-after']); | ||
if (is.nan(after)) { | ||
after = Date.parse(response.headers['retry-after']) - Date.now(); | ||
} else { | ||
after *= 1000; | ||
} | ||
|
||
if (after > retryOptions.maxRetryAfter) { | ||
return 0; | ||
} | ||
|
||
return after; | ||
} | ||
|
||
if (response && response.statusCode === 413) { | ||
return 0; | ||
} | ||
|
||
const noise = Math.random() * 100; | ||
return ((2 ** (attemptCount - 1)) * 1000) + noise; | ||
}; | ||
|
||
export default calculateRetryDelay; |
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
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
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