Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Feature: Improve retries mechanism #13

Merged
merged 4 commits into from
Sep 12, 2024
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
67 changes: 39 additions & 28 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,37 +199,29 @@ describe('Client', () => {
test('handle zero retries by default', async () => {
const client = new Client('id', 'key');

// Simulate server failure on first 2 attempts and success on the third
let retryCounter = 0;
mock.onGet('/products/:count').reply(() => {
retryCounter++;
if (retryCounter < 2) {
throw new Error('Internal Server Error');
}
return [200, 42];
});
// Simulate timeout error
mock.onGet('/products/:count').timeoutOnce();

await expect(
client.request(HttpMethod.get, '/products/:count', {}),
).rejects.toThrow(new Error('Internal Server Error'));
).rejects.toThrow(new Error('timeout of 0ms exceeded'));
});

test('handle retries option', async () => {
const client = new Client('id', 'key', { retries: 3 });

// Simulate server failure on first 2 attempts and success on the third
let retryCounter = 0;
mock.onGet('/products/:count').reply(() => {
retryCounter++;
if (retryCounter < 2) {
throw new Error('Internal Server Error');
}
return [200, 42];
});
mock
.onGet('/products:variants/:count')
.timeoutOnce()
.onGet('/products:variants/:count')
.timeoutOnce()
.onGet('/products:variants/:count')
.replyOnce(200, 42);

const response = await client.request(
HttpMethod.get,
'/products/:count',
'/products:variants/:count',
{},
);
expect(response).toEqual(42);
Expand All @@ -239,18 +231,37 @@ describe('Client', () => {
const client = new Client('id', 'key', { retries: 3 });

// Simulate server failure on first 4 attempts and success on the fifth
let retryCounter = 0;
mock.onGet('/products/:count').reply(() => {
retryCounter++;
if (retryCounter < 5) {
throw new Error('Internal Server Error');
}
return [200, 42];
mock
.onGet('/categories/:count')
.timeoutOnce()
.onGet('/categories/:count')
.timeoutOnce()
.onGet('/categories/:count')
.timeoutOnce()
.onGet('/categories/:count')
.timeoutOnce()
.onGet('/categories/:count')
.replyOnce(200, 42);

await expect(
client.request(HttpMethod.get, '/categories/:count', {}),
).rejects.toThrow(new Error('timeout of 0ms exceeded'));
});

test('handle return error code without retries', async () => {
const client = new Client('id', 'key', { retries: 3 });

// Simulate server returns 404 error with 1st attempt
let attemptsCouter = 0;
mock.onGet('/:files/robots.txt').reply(() => {
attemptsCouter++;
return [404, 'Not found'];
});

await expect(
client.request(HttpMethod.get, '/products/:count', {}),
).rejects.toThrow(new Error('Internal Server Error'));
client.request(HttpMethod.get, '/:files/robots.txt', {}),
).rejects.toThrow();
expect(attemptsCouter).toBe(1);
});
}); // describe: #retry
});
14 changes: 12 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class ApiError extends Error {
}
}

// We should retry request only in case of timeout or disconnect
const RETRY_CODES = new Set(['ECONNABORTED', 'ECONNREFUSED']);

/**
* Swell API Client.
*/
Expand Down Expand Up @@ -189,10 +192,17 @@ export class Client {
const response = await this.httpClient.request<T>(requestParams);
resolve(transformResponse(response).data);
} catch (error) {
if (operation.retry(error as Error)) {
// Attempt retry if we encounter a timeout or connection error
const code = axios.isAxiosError(error) ? error?.code : null;
DimaZhukovsky marked this conversation as resolved.
Show resolved Hide resolved

if (
code &&
RETRY_CODES.has(code) &&
operation.retry(error as Error)
) {
return;
}
reject(transformError(operation.mainError()));
reject(transformError(error));
}
});
});
Expand Down
Loading