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: add initial retry delay, and set default to 100ms #336

Merged
merged 3 commits into from
Sep 14, 2020
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ gaxios.request({url: '/data'}).then(...);

// When there is no response, the number of retries to attempt. Defaults to 2.
noResponseRetries?: number;

// The amount of time to initially delay the retry, in ms. Defaults to 100ms.
retryDelay?: number;
},

// Enables default configuration for retries.
Expand Down
3 changes: 1 addition & 2 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ export interface RetryConfig {
currentRetryAttempt?: number;

/**
* The amount of time to initially delay the retry. Defaults to 100.
* @deprecated
* The amount of time to initially delay the retry, in ms. Defaults to 100ms.
*/
retryDelay?: number;

Expand Down
7 changes: 5 additions & 2 deletions src/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ export async function getRetryConfig(err: GaxiosError) {
}

// Calculate time to wait with exponential backoff.
// Formula: (2^c - 1 / 2) * 1000
const delay = ((Math.pow(2, config.currentRetryAttempt) - 1) / 2) * 1000;
// If this is the first retry, look for a configured retryDelay.
const retryDelay = config.currentRetryAttempt ? 0 : config.retryDelay ?? 100;
// Formula: retryDelay + ((2^c - 1 / 2) * 1000)
const delay =
retryDelay + ((Math.pow(2, config.currentRetryAttempt) - 1) / 2) * 1000;

// We're going to retry! Incremenent the counter.
err.config.retryConfig!.currentRetryAttempt! += 1;
Expand Down
26 changes: 26 additions & 0 deletions test/test.retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,30 @@ describe('🛸 retry & exponential backoff', () => {
});
scope.done();
});

it('should delay the initial retry by 100ms by default', async () => {
const scope = nock(url).get('/').reply(500).get('/').reply(200, {});
const start = Date.now();
await request({
url,
retry: true,
});
const delay = Date.now() - start;
assert.ok(delay > 100 && delay < 150);
scope.done();
});

it('should respect the retryDelay if configured', async () => {
const scope = nock(url).get('/').reply(500).get('/').reply(200, {});
const start = Date.now();
await request({
url,
retryConfig: {
retryDelay: 500,
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like a nice simple way to write this test for now 👍

I've definitely seen tests like this that add 100+ ms delays add up over time, and lead to a fairly slow test suite... I don't think this will bite us, having a couple smoke tests for retry logic.

},
});
const delay = Date.now() - start;
assert.ok(delay > 500 && delay < 550);
scope.done();
});
});