retry
Advanced tools
Comparing version 0.6.1 to 0.7.0
@@ -6,3 +6,3 @@ var dns = require('dns'); | ||
var opts = { | ||
times: 2, | ||
retries: 2, | ||
factor: 2, | ||
@@ -32,2 +32,2 @@ minTimeout: 1 * 1000, | ||
console.log(addresses); | ||
}); | ||
}); |
@@ -1,2 +0,2 @@ | ||
function RetryOperation(timeouts) { | ||
function RetryOperation(timeouts, retryForever) { | ||
this._timeouts = timeouts; | ||
@@ -9,2 +9,6 @@ this._fn = null; | ||
this._timeout = null; | ||
if (!!retryForever) { | ||
this._cachedTimeouts = this._timeouts.slice(0); | ||
} | ||
} | ||
@@ -26,10 +30,15 @@ module.exports = RetryOperation; | ||
if (timeout === undefined) { | ||
return false; | ||
if (this._cachedTimeouts) { | ||
// retry forever, only keep last error | ||
this._errors.splice(this._errors.length - 1, this._errors.length); | ||
this._timeouts = this._cachedTimeouts.slice(0); | ||
timeout = this._timeouts.shift(); | ||
} else { | ||
return false; | ||
} | ||
} | ||
this._attempts++; | ||
var self = this; | ||
setTimeout(function() { | ||
self._fn(self._attempts); | ||
self._attempts++; | ||
@@ -41,2 +50,4 @@ if (self._operationTimeoutCb) { | ||
} | ||
self._fn(self._attempts); | ||
}, timeout); | ||
@@ -59,4 +70,2 @@ | ||
this._fn(this._attempts); | ||
var self = this; | ||
@@ -68,2 +77,4 @@ if (this._operationTimeoutCb) { | ||
} | ||
this._fn(this._attempts); | ||
}; | ||
@@ -114,2 +125,2 @@ | ||
return mainError; | ||
}; | ||
}; |
var RetryOperation = require('./retry_operation'); | ||
exports.operation = function(options) { | ||
var retryForever = false; | ||
if (options && options.forever === true) retryForever = true; | ||
var timeouts = exports.timeouts(options); | ||
return new RetryOperation(timeouts); | ||
return new RetryOperation(timeouts, retryForever); | ||
}; | ||
@@ -30,3 +32,3 @@ | ||
for (var i = 0; i < opts.retries; i++) { | ||
timeouts.push(this._createTimeout(i, opts)); | ||
timeouts.push(this.createTimeout(i, opts)); | ||
} | ||
@@ -42,3 +44,3 @@ | ||
exports._createTimeout = function(attempt, opts) { | ||
exports.createTimeout = function(attempt, opts) { | ||
var random = (opts.randomize) | ||
@@ -52,2 +54,2 @@ ? (Math.random() + 1) | ||
return timeout; | ||
}; | ||
}; |
@@ -5,3 +5,4 @@ { | ||
"description": "Abstraction for exponential and custom retry strategies for failed operations.", | ||
"version": "0.6.1", | ||
"license": "MIT", | ||
"version": "0.7.0", | ||
"homepage": "https://github.com/tim-kos/node-retry", | ||
@@ -8,0 +9,0 @@ "repository": { |
@@ -99,6 +99,17 @@ # retry | ||
[http://www.wolframalpha.com/input/?i=Sum%5B1000*x^k%2C+{k%2C+0%2C+9}%5D+%3D+5+*+60+*+1000]() | ||
<http://www.wolframalpha.com/input/?i=Sum%5B1000*x^k%2C+{k%2C+0%2C+9}%5D+%3D+5+*+60+*+1000> | ||
[article]: http://dthain.blogspot.com/2009/02/exponential-backoff-in-distributed.html | ||
### retry.createTimeout(attempt, opts) | ||
Returns a new `timeout` (integer in milliseconds) based on the given parameters. | ||
`attempt` is an integer representing for which retry the timeout should be calculated. If your retry operation was executed 4 times you had one attempt and 3 retries. If you then want to calculate a new timeout, you should set `attempt` to 4 (attempts are zero-indexed). | ||
`opts` can include `factor`, `minTimeout`, `randomize` (boolean) and `maxTimeout`. They are documented above. | ||
`retry.createTimeout()` is used internally by `retry.timeouts()` and is public for you to be able to create your own timeouts for reinserting an item, see [issue #13](https://github.com/tim-kos/node-retry/issues/13). | ||
### new RetryOperation(timeouts) | ||
@@ -105,0 +116,0 @@ |
@@ -80,2 +80,28 @@ var common = require('../common'); | ||
fn(); | ||
})(); | ||
})(); | ||
(function testRetryForever() { | ||
var error = new Error('some error'); | ||
var operation = retry.operation({ retries: 3, forever: true }); | ||
var attempts = 0; | ||
var finalCallback = fake.callback('finalCallback'); | ||
fake.expectAnytime(finalCallback); | ||
var fn = function() { | ||
operation.attempt(function(currentAttempt) { | ||
attempts++; | ||
assert.equal(currentAttempt, attempts); | ||
if (attempts !== 6 && operation.retry(error)) { | ||
return; | ||
} | ||
assert.strictEqual(attempts, 6); | ||
assert.strictEqual(operation.attempts(), attempts); | ||
assert.strictEqual(operation.mainError(), error); | ||
finalCallback(); | ||
}); | ||
}; | ||
fn(); | ||
})(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
18991
318
179