From 07b3ce5070515c1a4d1462d2c046ef13b805e812 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Thu, 8 Nov 2018 17:14:19 +0100 Subject: [PATCH] Fix the retry math (#653) --- readme.md | 2 +- source/normalize-arguments.js | 2 +- source/request-as-event-emitter.js | 4 +--- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index bc308c0d3..4529b7f6c 100644 --- a/readme.md +++ b/readme.md @@ -264,7 +264,7 @@ An object representing `retries`, `methods`, `statusCodes` and `maxRetryAfter` f If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`.
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). diff --git a/source/normalize-arguments.js b/source/normalize-arguments.js index b1f4de2a4..37ed8dbfc 100644 --- a/source/normalize-arguments.js +++ b/source/normalize-arguments.js @@ -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; }; } diff --git a/source/request-as-event-emitter.js b/source/request-as-event-emitter.js index d706367c9..c579575c6 100644 --- a/source/request-as-event-emitter.js +++ b/source/request-as-event-emitter.js @@ -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; @@ -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; @@ -241,7 +240,6 @@ module.exports = (options, input) => { await hook(options, error, retryCount); } - retryCount++; await get(options); } catch (error) { emitter.emit('error', error);