From 4a344678110864d97818a8272ebcc5e1c4921b52 Mon Sep 17 00:00:00 2001 From: Camron Flanders Date: Mon, 6 Mar 2023 14:43:39 -0600 Subject: [PATCH] feat: Add support for custom backoff (#498) * Add support for custom retry strategies * improve docs * revert changes to README * formatting --------- Co-authored-by: Denis DelGrosso <85250797+ddelgrosso1@users.noreply.github.com> --- src/common.ts | 7 +++++++ src/retry.ts | 8 +++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/common.ts b/src/common.ts index 2915b27..4513cdc 100644 --- a/src/common.ts +++ b/src/common.ts @@ -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; } export type FetchImplementation = ( diff --git a/src/retry.ts b/src/retry.ts index a4462a9..540298e 100644 --- a/src/retry.ts +++ b/src/retry.ts @@ -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) {