Skip to content

Commit

Permalink
Fix the retry math (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak authored and sindresorhus committed Nov 8, 2018
1 parent d8dd881 commit 07b3ce5
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 5 deletions.
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -264,7 +264,7 @@ An object representing `retries`, `methods`, `statusCodes` and `maxRetryAfter` f
If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`.<br>
If [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header is greater than `maxRetryAfter`, it will cancel the request.

Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 0).
Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 1).

The `retries` property can be a `number` or a `function` with `retry` and `error` arguments. The function must return a delay in milliseconds (`0` return value cancels retry).

Expand Down
2 changes: 1 addition & 1 deletion source/normalize-arguments.js
Expand Up @@ -236,7 +236,7 @@ const normalize = (url, options, defaults) => {
}

const noise = Math.random() * 100;
return ((1 << iteration) * 1000) + noise;
return ((2 ** (iteration - 1)) * 1000) + noise;
};
}

Expand Down
4 changes: 1 addition & 3 deletions source/request-as-event-emitter.js
Expand Up @@ -27,7 +27,6 @@ module.exports = (options, input) => {
let redirectString;
let uploadBodySize;
let retryCount = 0;
let retryTries = 0;
let shouldAbort = false;

const setCookie = options.cookieJar ? util.promisify(options.cookieJar.setCookie.bind(options.cookieJar)) : null;
Expand Down Expand Up @@ -227,7 +226,7 @@ module.exports = (options, input) => {
let backoff;

try {
backoff = options.retry.retries(++retryTries, error);
backoff = options.retry.retries(++retryCount, error);
} catch (error2) {
emitter.emit('error', error2);
return;
Expand All @@ -241,7 +240,6 @@ module.exports = (options, input) => {
await hook(options, error, retryCount);
}

retryCount++;
await get(options);
} catch (error) {
emitter.emit('error', error);
Expand Down

0 comments on commit 07b3ce5

Please sign in to comment.