Skip to content

Commit

Permalink
feat: configurable maxRetryDelay (#165)
Browse files Browse the repository at this point in the history
Co-authored-by: Justin Beckwith <beckwith@google.com>
  • Loading branch information
barrett-schonefeld and JustinBeckwith committed Aug 23, 2021
1 parent e63ca08 commit b8842d7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export interface RetryConfig {
* Max permitted Retry-After value (in ms) - rejects if greater. Defaults to 5 mins.
*/
maxRetryAfter?: number;

/**
* Ceiling for calculated delay (in ms) - delay will not exceed this value.
*/
maxRetryDelay?: number;
}

export type RaxConfig = {
Expand Down Expand Up @@ -258,6 +263,9 @@ function onError(err: AxiosError) {
} else {
delay = ((Math.pow(2, retrycount) - 1) / 2) * 1000;
}
if (typeof config.maxRetryDelay === 'number') {
delay = Math.min(delay, config.maxRetryDelay);
}
}
setTimeout(resolve, delay);
});
Expand Down
27 changes: 27 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,33 @@ describe('retry-axios', () => {
await assert.rejects(axios(cfg));
assert.strictEqual(scopes[1].isDone(), false);
});

it('should use maxRetryDelay', async function () {
this.timeout(1000); // Short timeout to trip test if delay longer than expected
const scopes = [
nock(url).get('/').reply(429, undefined),
nock(url).get('/').reply(200, 'toast'),
];
interceptorId = rax.attach();
const {promise, resolve} = invertedPromise();
const clock = sinon.useFakeTimers({
shouldAdvanceTime: true, // Otherwise interferes with nock
});
const axiosPromise = axios({
url,
raxConfig: {
onRetryAttempt: resolve,
retryDelay: 10000, // Higher default to ensure maxRetryDelay is used
maxRetryDelay: 5000,
backoffType: 'exponential',
},
});
await promise;
clock.tick(5000); // Advance clock by expected retry delay
const res = await axiosPromise;
assert.strictEqual(res.data, 'toast');
scopes.forEach(s => s.done());
});
});

function invertedPromise() {
Expand Down

0 comments on commit b8842d7

Please sign in to comment.