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: exponential retries for axiosWrapper #980

Merged
merged 1 commit into from
Nov 8, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
AxiosWrapper,
bearerTokenAuthorizationIsRequiredErrorMsg,
} from '@/services/axios-wrapper/axiosWrapper';
import * as helpers from '@/services/helpers/delay';

// mute console logs
console.log = jest.fn();
Expand All @@ -26,12 +27,18 @@ describe('axiosWrapper', () => {
let axiosInstance: AxiosInstance;
let axiosGetMock: jest.Mock;
let axiosGetSpy: jest.SpyInstance;
let delaySpy: jest.SpyInstance;

beforeEach(() => {
axiosInstance = mockAxios;
axiosGetMock = jest.fn();
axiosInstance.get = axiosGetMock;
axiosGetSpy = jest.spyOn(axiosInstance, 'get');
delaySpy = jest.spyOn(helpers, 'delay').mockResolvedValue();
});

afterEach(() => {
jest.clearAllMocks();
});

it('should retry 0 time with Bearer Token Authorization is required error message', async () => {
Expand Down Expand Up @@ -196,6 +203,22 @@ describe('axiosWrapper', () => {
expect(axiosGetSpy).toBeCalledTimes(4);
}
});

it('should have retry delay increase exponentially', async () => {
axiosGetMock
.mockRejectedValueOnce(new Error('error 1'))
.mockRejectedValueOnce(new Error('error 2'))
.mockRejectedValueOnce(new Error('error 3'))
.mockRejectedValue(new Error('error 4'));

try {
await new AxiosWrapper(axiosInstance).get('some-url');
fail('should fail');
} catch (e: any) {
expect(delaySpy).toBeCalledTimes(3);
expect(delaySpy.mock.calls).toEqual([[500], [1000], [2000]]);
}
});
});

function createAxiosResponseError(message: string): { response: AxiosResponse } {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const bearerTokenAuthorizationIsRequiredErrorMsg = 'Bearer Token Authoriz

export class AxiosWrapper {
protected readonly retryCount = 3;
protected readonly retryDelay = 500;
protected readonly base = 2;
protected readonly initialDelay = 500;
protected readonly axiosInstance: AxiosInstance;
protected readonly errorMessagesToRetry?: string;

Expand Down Expand Up @@ -105,9 +106,11 @@ export class AxiosWrapper {
throw err;
}

// Retry the request after a delay.
console.warn(`Retrying request to ${url} in ${this.retryDelay} ms, ${retry} left`);
await delay(this.retryDelay);
// Retry the request after an exponential delay.
const exp = this.retryCount - retry;
const expDelay = this.initialDelay * this.base ** exp;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as

 this.initialDelay * (this.base ** exp);

but prettier complains about the extra brackets

console.warn(`Retrying request to ${url} in ${expDelay} ms, ${retry} left`);
await delay(expDelay);

return await this.doRetryFunc(fun, url, --retry);
}
Expand Down
Loading