From bde7aae95d3f7614a94cb1476abef77c07648eeb Mon Sep 17 00:00:00 2001 From: Camron Flanders Date: Mon, 15 Aug 2022 12:27:38 -0500 Subject: [PATCH 1/4] Add support for custom retry strategies --- src/common.ts | 5 +++++ src/retry.ts | 10 ++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/common.ts b/src/common.ts index 2915b271..d64c74ea 100644 --- a/src/common.ts +++ b/src/common.ts @@ -159,6 +159,11 @@ export interface RetryConfig { * When there is no response, the number of retries to attempt. Defaults to 2. */ noResponseRetries?: number; + + /** + * Function to invoke which determines how long to delay the next retry. + */ + retryBackoff?: (err: GaxiosError, defaultBackoffMs: number) => Promise; } export type FetchImplementation = ( diff --git a/src/retry.ts b/src/retry.ts index a4462a95..f61ea9ed 100644 --- a/src/retry.ts +++ b/src/retry.ts @@ -70,10 +70,12 @@ 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) { config.onRetryAttempt(err); From 878f8d06ac43e3c1b7c9e95c001ec3554d8bb1b2 Mon Sep 17 00:00:00 2001 From: Camron Flanders Date: Mon, 15 Aug 2022 13:09:05 -0500 Subject: [PATCH 2/4] improve docs --- README.md | 5 +++++ src/common.ts | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 74f13ad2..19a0ef72 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,11 @@ over other authentication methods, i.e., application default credentials. // The amount of time to initially delay the retry, in ms. Defaults to 100ms. retryDelay?: 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; }, // Enables default configuration for retries. diff --git a/src/common.ts b/src/common.ts index d64c74ea..eeb974d7 100644 --- a/src/common.ts +++ b/src/common.ts @@ -161,7 +161,9 @@ export interface RetryConfig { noResponseRetries?: number; /** - * Function to invoke which determines how long to delay the next retry. + * 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; } From 81aff410cbcc12fdc6c435662cac395c0e8c4126 Mon Sep 17 00:00:00 2001 From: Camron Flanders Date: Mon, 15 Aug 2022 13:39:25 -0500 Subject: [PATCH 3/4] revert changes to README --- README.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/README.md b/README.md index 19a0ef72..74f13ad2 100644 --- a/README.md +++ b/README.md @@ -144,11 +144,6 @@ over other authentication methods, i.e., application default credentials. // The amount of time to initially delay the retry, in ms. Defaults to 100ms. retryDelay?: 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; }, // Enables default configuration for retries. From f430e691c70120cbf96b03550fbe0c340b1a8b67 Mon Sep 17 00:00:00 2001 From: Camron Flanders Date: Mon, 6 Mar 2023 10:41:17 -0600 Subject: [PATCH 4/4] formatting --- src/common.ts | 4 ++-- src/retry.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/common.ts b/src/common.ts index eeb974d7..4513cdc3 100644 --- a/src/common.ts +++ b/src/common.ts @@ -159,13 +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; + retryBackoff?: (err: GaxiosError, defaultBackoffMs: number) => Promise; } export type FetchImplementation = ( diff --git a/src/retry.ts b/src/retry.ts index f61ea9ed..540298e8 100644 --- a/src/retry.ts +++ b/src/retry.ts @@ -72,10 +72,10 @@ export async function getRetryConfig(err: GaxiosError) { // Create a promise that invokes the retry after the backOffDelay const backoff = config.retryBackoff ? config.retryBackoff(err, delay) - : new Promise((resolve) => { + : new Promise(resolve => { setTimeout(resolve, delay); }); - + // Notify the user if they added an `onRetryAttempt` handler if (config.onRetryAttempt) { config.onRetryAttempt(err);