Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: tim-kos/node-retry
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: f802d9edc2fdbca727d3e368234b6d714db06f8e
Choose a base ref
...
head repository: tim-kos/node-retry
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b8e26ea7adda3f11c19086a762d15e95521f3e4e
Choose a head ref
  • 9 commits
  • 5 files changed
  • 9 contributors

Commits on Nov 6, 2018

  1. Fix logical mistake (#61)

    sonicdoe authored and tim-kos committed Nov 6, 2018
    Copy the full SHA
    cc0559b View commit details
  2. Use SVG for Travis CI badge (#62)

    sonicdoe authored and tim-kos committed Nov 6, 2018
    Copy the full SHA
    88548b9 View commit details

Commits on Feb 26, 2020

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    2424324 View commit details

Commits on Mar 6, 2020

  1. Stop retry timer with operation.stop() (#69)

    Fix tim-kos/node-retry/#64
    danielfdsilva authored Mar 6, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    c402d77 View commit details

Commits on Aug 10, 2020

  1. Update retry_operation.js (#74)

    Fixed the slice operation so that this comment is true:
    ```
    // retry forever, only keep last error
    ```
    chanced authored Aug 10, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    b316bfc View commit details

Commits on Dec 16, 2020

  1. Avoid infinite loop and patch exponential backoff edge case (#68)

    * Don't break exponential backoff when minTimeout is zero
    
    * Hope this is how versioning works on this project!
    
    * No more infinite loops when innocently setting retries to Infinity
    gittyeric authored Dec 16, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    bf0ae23 View commit details
  2. Handle errors despite timeouts (#71)

    * Ensures that timeouts don't prevent errors from getting into the _errors array.
    
    * Replaces a test that used internal knowledge with a test targeted towards actual use cases.
    
    Co-authored-by: Danny Rivers <drivers@roirevolution.com>
    cdeevfrr and Danny Rivers authored Dec 16, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    a15d08e View commit details
  3. fixed retry() and forever (#76)

    * fixed retry()
    
    * fixed forever
    
    * Update package.json
    wizo06 authored Dec 16, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    a7cedfb View commit details

Commits on Jun 21, 2021

  1. Release 0.13.1

    tim-kos committed Jun 21, 2021
    Copy the full SHA
    b8e26ea View commit details
Showing with 55 additions and 12 deletions.
  1. +2 −2 README.md
  2. +2 −2 lib/retry.js
  3. +10 −6 lib/retry_operation.js
  4. +6 −2 package.json
  5. +35 −0 test/integration/test-retry-operation.js
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- badges/ -->
[![Build Status](https://secure.travis-ci.org/tim-kos/node-retry.png?branch=master)](http://travis-ci.org/tim-kos/node-retry "Check this project's build status on TravisCI")
[![Build Status](https://secure.travis-ci.org/tim-kos/node-retry.svg?branch=master)](http://travis-ci.org/tim-kos/node-retry "Check this project's build status on TravisCI")
[![codecov](https://codecov.io/gh/tim-kos/node-retry/branch/master/graph/badge.svg)](https://codecov.io/gh/tim-kos/node-retry)
<!-- /badges -->

@@ -62,7 +62,7 @@ var operation = retry.operation({

### retry.operation([options])

Creates a new `RetryOperation` object. `options` is the same as `retry.timeouts()`'s `options`, with two additions:
Creates a new `RetryOperation` object. `options` is the same as `retry.timeouts()`'s `options`, with three additions:

* `forever`: Whether to retry forever, defaults to `false`.
* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) the setTimeout's, defaults to `false`.
4 changes: 2 additions & 2 deletions lib/retry.js
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ var RetryOperation = require('./retry_operation');
exports.operation = function(options) {
var timeouts = exports.timeouts(options);
return new RetryOperation(timeouts, {
forever: options && options.forever,
forever: options && (options.forever || options.retries === Infinity),
unref: options && options.unref,
maxRetryTime: options && options.maxRetryTime
});
@@ -51,7 +51,7 @@ exports.createTimeout = function(attempt, opts) {
? (Math.random() + 1)
: 1;

var timeout = Math.round(random * opts.minTimeout * Math.pow(opts.factor, attempt));
var timeout = Math.round(random * Math.max(opts.minTimeout, 1) * Math.pow(opts.factor, attempt));
timeout = Math.min(timeout, opts.maxTimeout);

return timeout;
16 changes: 10 additions & 6 deletions lib/retry_operation.js
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ function RetryOperation(timeouts, options) {
this._operationTimeoutCb = null;
this._timeout = null;
this._operationStart = null;
this._timer = null;

if (this._options.forever) {
this._cachedTimeouts = this._timeouts.slice(0);
@@ -24,13 +25,16 @@ module.exports = RetryOperation;

RetryOperation.prototype.reset = function() {
this._attempts = 1;
this._timeouts = this._originalTimeouts;
this._timeouts = this._originalTimeouts.slice(0);
}

RetryOperation.prototype.stop = function() {
if (this._timeout) {
clearTimeout(this._timeout);
}
if (this._timer) {
clearTimeout(this._timer);
}

this._timeouts = [];
this._cachedTimeouts = null;
@@ -46,6 +50,7 @@ RetryOperation.prototype.retry = function(err) {
}
var currentTime = new Date().getTime();
if (err && currentTime - this._operationStart >= this._maxRetryTime) {
this._errors.push(err);
this._errors.unshift(new Error('RetryOperation timeout occurred'));
return false;
}
@@ -56,16 +61,15 @@ RetryOperation.prototype.retry = function(err) {
if (timeout === undefined) {
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();
this._errors.splice(0, this._errors.length - 1);
timeout = this._cachedTimeouts.slice(-1);
} else {
return false;
}
}

var self = this;
var timer = setTimeout(function() {
this._timer = setTimeout(function() {
self._attempts++;

if (self._operationTimeoutCb) {
@@ -82,7 +86,7 @@ RetryOperation.prototype.retry = function(err) {
}, timeout);

if (this._options.unref) {
timer.unref();
this._timer.unref();
}

return true;
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -3,16 +3,20 @@
"name": "retry",
"description": "Abstraction for exponential and custom retry strategies for failed operations.",
"license": "MIT",
"version": "0.12.0",
"version": "0.13.1",
"homepage": "https://github.com/tim-kos/node-retry",
"repository": {
"type": "git",
"url": "git://github.com/tim-kos/node-retry.git"
},
"files": [
"lib",
"example"
],
"directories": {
"lib": "./lib"
},
"main": "index",
"main": "index.js",
"engines": {
"node": ">= 4"
},
35 changes: 35 additions & 0 deletions test/integration/test-retry-operation.js
Original file line number Diff line number Diff line change
@@ -256,3 +256,38 @@ var retry = require(common.dir.lib + '/retry');

fn();
})();

(function testErrorsPreservedWhenMaxRetryTimeExceeded() {
var error = new Error('some error');
var maxRetryTime = 30;
var operation = retry.operation({
minTimeout: 1,
maxRetryTime: maxRetryTime
});

var finalCallback = fake.callback('finalCallback');
fake.expectAnytime(finalCallback);

var longAsyncFunction = function (wait, callback){
setTimeout(callback, wait);
};

var fn = function() {
var startTime = new Date().getTime();
operation.attempt(function() {

var curTime = new Date().getTime();
longAsyncFunction(maxRetryTime - (curTime - startTime - 1), function(){
if (operation.retry(error)) {
assert.fail('timeout should be occurred');
return;
}

assert.strictEqual(operation.mainError(), error);
finalCallback();
});
});
};

fn();
})();