Skip to content

Commit 2e11e1d

Browse files
committed
Merge pull request #139 from sindresorhus/retries-function
Accept backoff function in retry option
2 parents 54bd6f5 + a10a99e commit 2e11e1d

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ var nodeStatusCodes = require('node-status-codes');
1919
var isPlainObj = require('is-plain-obj');
2020
var parseJson = require('parse-json');
2121

22-
function backoff(iter) {
23-
var noise = Math.random() * 100;
24-
return (1 << iter) * 1000 + noise;
25-
}
26-
2722
function requestAsEventEmitter(opts) {
2823
opts = opts || {};
2924

@@ -61,8 +56,9 @@ function requestAsEventEmitter(opts) {
6156
});
6257

6358
req.once('error', function (err) {
64-
if (retryCount < opts.retries) {
65-
setTimeout(get, backoff(++retryCount), opts);
59+
var backoff = opts.retries(++retryCount);
60+
if (backoff) {
61+
setTimeout(get, backoff, opts);
6662
return;
6763
}
6864

@@ -270,6 +266,18 @@ function normalizeArguments(url, opts) {
270266
}
271267
}
272268

269+
if (typeof opts.retries !== 'function') {
270+
var retries = opts.retries;
271+
opts.retries = function backoff(iter) {
272+
if (iter > retries) {
273+
return 0;
274+
}
275+
276+
var noise = Math.random() * 100;
277+
return (1 << iter) * 1000 + noise;
278+
};
279+
}
280+
273281
return opts;
274282
}
275283

readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ Milliseconds after which the request will be aborted and an error event with `ET
118118

119119
###### retries
120120

121-
Type: `number`
121+
Type: `number`, `function`
122122
Default: `5`
123123

124-
Number of request retries when network errors happens.
124+
Number of request retries when network errors happens. Delays between retries counts with function `Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 0).
125125

126+
Option accepts `function` with `retry` argument that must return delay in milliseconds (`0` return value cancels retry).
126127

127128
##### callback(error, data, response)
128129

test/retry.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {createServer} from './_server';
55
let s;
66
let trys = 0;
77
let knocks = 0;
8+
let fifth = 0;
89

910
test.before('setup', async t => {
1011
s = await createServer();
@@ -21,6 +22,12 @@ test.before('setup', async t => {
2122
trys++;
2223
});
2324

25+
s.on('/fifth', (req, res) => {
26+
if (fifth++ === 5) {
27+
res.end('who`s there?');
28+
}
29+
});
30+
2431
await s.listen(s.port);
2532
});
2633

@@ -38,6 +45,19 @@ test('can be disabled with option', async t => {
3845
t.is(trys, 1);
3946
});
4047

48+
test('funcion gets iter count', async t => {
49+
await got(`${s.url}/fifth`, {timeout: 100, retries: iter => iter < 10});
50+
t.is(fifth, 6);
51+
});
52+
53+
test('falsy value prevent retries', async t => {
54+
try {
55+
await got(`${s.url}/long`, {timeout: 1000, retries: () => 0});
56+
} catch (err) {
57+
t.ok(err);
58+
}
59+
});
60+
4161
test.after('cleanup', async t => {
4262
await s.close();
4363
});

0 commit comments

Comments
 (0)