From fd9857c66f7f2303e21131d22a28c8236e7be32c Mon Sep 17 00:00:00 2001 From: "Dr. Jan-Philip Gehrcke" Date: Mon, 16 Aug 2021 12:28:09 +0200 Subject: [PATCH] calc delay: mutate cfg on orig err obj Found that state is not retained across retries when mutating `config.currentRetryAttempt` here. --- src/index.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6ed03e6..c63be6d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -224,10 +224,20 @@ function onError(err: AxiosError) { } } - // Now it's certain that a retry is supposed to happen. - // Incremenent the counter, critical for linear and exp - // backoff delay calc. - config.currentRetryAttempt! += 1; + // Now it's certain that a retry is supposed to happen. Incremenent the + // counter, critical for linear and exp backoff delay calc. Note that + // `config.currentRetryAttempt` is local to this function whereas + // `(err.config as RaxConfig).raxConfig` is state that is tranferred across + // retries. That is, we want to mutate `(err.config as + // RaxConfig).raxConfig`. Another important note is about the definition of + // `currentRetryAttempt`: When we are here becasue the first and actual + // HTTP request attempt failed then `currentRetryAttempt` is still zero. We + // have found that a retry is indeed required. Since that is (will be) + // indeed the first retry it makes sense to now increase + // `currentRetryAttempt` by 1. So that it is in fact 1 for the first retry + // (as opposed to 0 or 2); an intuitive convention to use for the math + // below. + (err.config as RaxConfig).raxConfig!.currentRetryAttempt! += 1; // Calculate delay according to chosen strategy // Default to exponential backoff - formula: ((2^c - 1) / 2) * 1000