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

Allow custom retry interval #1161

Merged
merged 4 commits into from
May 21, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,7 @@ __Arguments__
* `opts` - Can be either an object with `times` and `interval` or a number.
* `times` - The number of attempts to make before giving up. The default is `5`.
* `interval` - The time to wait between retries, in milliseconds. The default is `0`.
The interval may also be specified as a function of the retry count.
* If `opts` is a number, the number specifies the number of times to retry, with the default interval of `0`.
* `task(callback, results)` - A function which receives two arguments: (1) a `callback(err, result)`
which must be called when finished, passing `err` (which can be `null`) and the `result` of
Expand All @@ -1595,6 +1596,21 @@ async.retry({times: 3, interval: 200}, apiMethod, function(err, result) {
});
```

The interval may also be specified as a function of the retry count:

```js
// try calling apiMethod 10 times with exponential backoff
// (i.e. intervals of 100, 200, 400, 800, 1600, ... milliseconds)
async.retry({
times: 10,
interval: function(retryCount) {
return 50 * Math.pow(2, retryCount);
}
}, apiMethod, function(err, result) {
// do something with the result
});
```

```js
// try calling apiMethod the default 5 times no delay between each retry
async.retry(apiMethod, function(err, result) {
Expand Down
35 changes: 25 additions & 10 deletions lib/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import series from './series';
import noop from 'lodash/noop';
import constant from 'lodash/constant';

/**
* Attempts to get a successful response from `task` no more than `times` times
Expand All @@ -18,7 +19,8 @@ import noop from 'lodash/noop';
* * `times` - The number of attempts to make before giving up. The default
* is `5`.
* * `interval` - The time to wait between retries, in milliseconds. The
* default is `0`.
* default is `0`. The interval may also be specified as a function of the
* retry count.
* * If `opts` is a number, the number specifies the number of times to retry,
* with the default interval of `0`.
* @param {Function} task - A function which receives two arguments: (1) a
Expand Down Expand Up @@ -46,7 +48,18 @@ import noop from 'lodash/noop';
* // do something with the result
* });
*
* // try calling apiMethod the default 5 times no delay between each retry
* // try calling apiMethod 10 times with exponential backoff
* // (i.e. intervals of 100, 200, 400, 800, 1600, ... milliseconds)
* async.retry({
* times: 10,
* interval: function(retryCount) {
* return 50 * Math.pow(2, retryCount);
* }
* }, apiMethod, function(err, result) {
* // do something with the result
* });
*
* // try calling apiMethod the default 5 times no delay between each retry
* async.retry(apiMethod, function(err, result) {
* // do something with the result
* });
Expand All @@ -64,16 +77,18 @@ export default function retry(times, task, callback) {
var DEFAULT_TIMES = 5;
var DEFAULT_INTERVAL = 0;


var opts = {
times: DEFAULT_TIMES,
interval: DEFAULT_INTERVAL
intervalFunc: constant(DEFAULT_INTERVAL)
};

function parseTimes(acc, t) {
if (typeof t === 'object') {
acc.times = +t.times || DEFAULT_TIMES;
acc.interval = +t.interval || DEFAULT_INTERVAL;

acc.intervalFunc = typeof t.interval === 'function' ?
t.interval :
constant(+t.interval || DEFAULT_INTERVAL);
} else if (typeof t === 'number' || typeof t === 'string') {
acc.times = +t || DEFAULT_TIMES;
} else {
Expand All @@ -95,13 +110,13 @@ export default function retry(times, task, callback) {
throw new Error("Invalid arguments for async.retry");
}


var attempts = [];
while (opts.times) {
var isFinalAttempt = !(opts.times -= 1);
for (var i = 1; i < opts.times + 1; i++) {
var isFinalAttempt = (i == opts.times);
Copy link
Collaborator

Choose a reason for hiding this comment

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

why the change to this loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I needed to pass the retryCount to the interval function but the loop was counting backwards and destroying opts.times.

attempts.push(retryAttempt(isFinalAttempt));
if (!isFinalAttempt && opts.interval > 0) {
attempts.push(retryInterval(opts.interval));
var interval = opts.intervalFunc(i);
if (!isFinalAttempt && interval > 0) {
attempts.push(retryInterval(interval));
}
}

Expand Down
26 changes: 24 additions & 2 deletions mocha_test/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("retry", function () {
});
});

it('retry when all attempts succeeds',function(done) {
it('retry when all attempts fail',function(done) {
var times = 3;
var callCount = 0;
var error = 'ERROR';
Expand Down Expand Up @@ -53,7 +53,7 @@ describe("retry", function () {
done();
});

it('retry with interval when all attempts succeeds',function(done) {
it('retry with interval when all attempts fail',function(done) {
var times = 3;
var interval = 50;
var callCount = 0;
Expand All @@ -75,6 +75,28 @@ describe("retry", function () {
});
});

it('retry with custom interval when all attempts fail',function(done) {
var times = 3;
var intervalFunc = function(retryCount) { return retryCount * 100; };
var callCount = 0;
var error = 'ERROR';
var erroredResult = 'RESULT';
function fn(callback) {
callCount++;
callback(error + callCount, erroredResult + callCount); // respond with indexed values
}
var start = new Date().getTime();
async.retry({ times: times, interval: intervalFunc}, fn, function(err, result){
var now = new Date().getTime();
var duration = now - start;
assert(duration >= 300, 'did not include custom interval');
assert.equal(callCount, 3, "did not retry the correct number of times");
assert.equal(err, error + times, "Incorrect error was returned");
assert.equal(result, erroredResult + times, "Incorrect result was returned");
done();
});
});

it("should not require a callback", function (done) {
var called = false;
async.retry(3, function(cb) {
Expand Down