Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor failed request callback to be agnostic of Bottleneck queue
- Loading branch information
1 parent
8af113a
commit 1e5d7f6
Showing
3 changed files
with
39 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,32 @@ | ||
import { getResetHeader } from './utils.js' | ||
import { isHttpError } from 'http-errors' | ||
import type Bottleneck from 'bottleneck' | ||
import type { RateLimitOptions, RetryOptions } from './options.d.ts' | ||
import type { Options } from './options.d.ts' | ||
|
||
const backoff = (retries: number) => Math.pow(retries + 1, 2) * 1000 | ||
|
||
export function handleFailed( | ||
options: RetryOptions & RateLimitOptions, | ||
error: Error, | ||
info: Bottleneck.EventInfoRetryable | ||
): number | void { | ||
const retry = info.retryCount < options.maxRetries | ||
|
||
if (retry) { | ||
if (isHttpError(error) && error?.response instanceof Response) { | ||
const response = error.response | ||
export function handleFailed(options: Options, error: Error, retries: number): number | void { | ||
if (retries >= options.maxRetries) { | ||
return | ||
} | ||
|
||
if (response.status === 429) { | ||
const wait = getResetHeader(response, options.rateLimitHeader, options.resetFormat) | ||
if (isHttpError(error) && error?.response instanceof Response) { | ||
if (options.doNotRetry?.includes(error.status)) { | ||
return | ||
} | ||
|
||
if (wait) { | ||
// Add extra 1 second to account for sub second differences | ||
return wait + 1000 | ||
} | ||
} | ||
if (error.status === 429) { | ||
const wait = getResetHeader(error.response, options.rateLimitHeader, options.resetFormat) | ||
|
||
if (!options.doNotRetry?.includes(response.status)) { | ||
return backoff(info.retryCount) | ||
if (wait) { | ||
// Add extra 1 second to account for sub second differences | ||
return wait + 1000 | ||
} | ||
} | ||
|
||
if (error.name === 'TimeoutError') { | ||
return backoff(info.retryCount) | ||
} | ||
return backoff(retries) | ||
} | ||
|
||
if (error.name === 'TimeoutError') { | ||
return backoff(retries) | ||
} | ||
} |