Skip to content

Commit

Permalink
feat: retry with exponential backoff (#501)
Browse files Browse the repository at this point in the history
* feat: retry with exponential backoff

* fix

* test
  • Loading branch information
williazz authored Mar 27, 2024
1 parent a915864 commit 59904c8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/dispatch/RetryHttpHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class RetryHttpHandler implements HttpHandler {
public constructor(
handler: HttpHandler,
retries: number,
backoff: BackoffFunction = (n) => n * 2000
backoff: BackoffFunction = (n) => 2000 * Math.pow(2, n - 1)
) {
this.handler = handler;
this.retries = retries;
Expand Down
21 changes: 15 additions & 6 deletions src/dispatch/__tests__/RetryHttpHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,17 @@ describe('RetryHttpHandler tests', () => {
expect(response).resolves.toBe(okStatus);
});

test('when request fails then retry succeeds after backoff', async () => {
test('when request fails then retry succeeds after exponential backoff', async () => {
// Init
jest.useFakeTimers({ legacyFakeTimers: true });
const badStatus = { response: { statusCode: 500 } };
const okStatus = { response: { statusCode: 200 } };
fetchHandler
.mockReturnValueOnce(Promise.resolve(badStatus))
.mockReturnValueOnce(Promise.resolve(badStatus))
.mockReturnValue(Promise.resolve(okStatus));

const retries = 1;
const retries = 2;
const retryHandler = new RetryHttpHandler(
new FetchHttpHandler(),
retries
Expand All @@ -187,18 +188,26 @@ describe('RetryHttpHandler tests', () => {

// Yield to the event queue so the handler can advance
await new Promise((resolve) => process.nextTick(resolve));
expect(fetchHandler).toHaveBeenCalledTimes(1);

// Advance the timer so the backoff timeout fires
jest.advanceTimersByTime(2000);
await new Promise((resolve) => process.nextTick(resolve));
expect(fetchHandler).toHaveBeenCalledTimes(2);

// Yield to the event queue so the handler can advance
// Advance the timer so the backoff timeout fires
jest.advanceTimersByTime(4000);
await new Promise((resolve) => process.nextTick(resolve));
expect(fetchHandler).toHaveBeenCalledTimes(3);

// Yield to the event queue so the handler can advance
await new Promise((resolve) => process.nextTick(resolve));
// Assert
expect(fetchHandler).toHaveBeenCalledTimes(2);

expect(await responsePromise).toBe(okStatus);
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 2000);
expect(setTimeout).toHaveBeenCalledTimes(2);
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 2000);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 4000);
});

test('when request fails then retry waits for backoff', async () => {
Expand Down

0 comments on commit 59904c8

Please sign in to comment.