Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: configurable maxRetryDelay #165

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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