Skip to content

Commit

Permalink
fix delay calc: non-zero between first two attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
jgehrcke committed Aug 16, 2021
1 parent 88261ca commit a4173be
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,19 +223,27 @@ function onError(err: AxiosError) {
return reject(err);
}
}
// Else calculate delay according to chosen strategy

// Now it's certain that a retry is supposed to happen.
// Incremenent the counter, critical for linear and exp
// backoff delay calc.
(err.config as RaxConfig).raxConfig!.currentRetryAttempt! += 1;

// Calculate delay according to chosen strategy
// Default to exponential backoff - formula: ((2^c - 1) / 2) * 1000
else {
if (delay === 0) { // was not set by Retry-After logic
if (config.backoffType === 'linear') {
// The delay between the first (actual) attempt and the
// first retry should be non-zero. That is, by definition
// config.currentRetryAttempt must equal to 1 for the first
// retry (was once 0-based, which is a bug -- see #122).
delay = config.currentRetryAttempt! * 1000;
} else if (config.backoffType === 'static') {
delay = config.retryDelay!;
} else {
delay = ((Math.pow(2, config.currentRetryAttempt!) - 1) / 2) * 1000;
}
}
// We're going to retry! Incremenent the counter.
(err.config as RaxConfig).raxConfig!.currentRetryAttempt! += 1;
setTimeout(resolve, delay);
});

Expand Down

0 comments on commit a4173be

Please sign in to comment.