Skip to content

Commit

Permalink
feat: Add support for custom backoff (#498)
Browse files Browse the repository at this point in the history
* Add support for custom retry strategies

* improve docs

* revert changes to README

* formatting

---------

Co-authored-by: Denis DelGrosso <[email protected]>
  • Loading branch information
camflan and ddelgrosso1 committed Mar 6, 2023
1 parent cf19f9b commit 4a34467
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ export interface RetryConfig {
* When there is no response, the number of retries to attempt. Defaults to 2.
*/
noResponseRetries?: number;

/**
* Function to invoke which returns a promise. After the promise resolves,
* the retry will be triggered. If provided, this will be used in-place of
* the `retryDelay`
*/
retryBackoff?: (err: GaxiosError, defaultBackoffMs: number) => Promise<void>;
}

export type FetchImplementation = (
Expand Down
8 changes: 5 additions & 3 deletions src/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ export async function getRetryConfig(err: GaxiosError) {
err.config.retryConfig!.currentRetryAttempt! += 1;

// Create a promise that invokes the retry after the backOffDelay
const backoff = new Promise(resolve => {
setTimeout(resolve, delay);
});
const backoff = config.retryBackoff
? config.retryBackoff(err, delay)
: new Promise(resolve => {
setTimeout(resolve, delay);
});

// Notify the user if they added an `onRetryAttempt` handler
if (config.onRetryAttempt) {
Expand Down

0 comments on commit 4a34467

Please sign in to comment.