requestretry
Advanced tools
Comparing version 0.0.1 to 1.0.0
61
index.js
'use strict'; | ||
/* | ||
* retriablerequest | ||
* Request | ||
* | ||
@@ -10,4 +10,5 @@ * Copyright(c) 2014 Francois-Guillaume Ribreau <npm@fgribreau.com> | ||
*/ | ||
var request = require('request'); | ||
var _ = require('fg-lodash'); | ||
var request = require('request'); | ||
var _ = require('fg-lodash'); | ||
var Cancelable = require('cancelable'); | ||
@@ -21,24 +22,48 @@ var RETRIABLE_ERRORS = ['ECONNRESET', 'ENOTFOUND', 'ESOCKETTIMEDOUT', 'ETIMEDOUT', 'ECONNREFUSED']; | ||
function isRetriableRequest(err, response){ | ||
// Inspired from https://github.com/geoffreak/request-enhanced/blob/master/src/request-enhanced.coffee#L107 | ||
return (err && _.contains(RETRIABLE_ERRORS, err.code)) || (response && 500 <= response.statusCode && response.statusCode < 600); | ||
function Request(options, f, maxAttempts, retryDelay){ | ||
this.maxAttempts = maxAttempts; | ||
this.retryDelay = retryDelay; | ||
this.options = options; | ||
this.f = _.once(f); | ||
this._timeout = null; | ||
this._req = null; | ||
} | ||
function tryUntilFail(options, f, retryOptions){ | ||
retryOptions.maxAttempts--; | ||
Request.request = request; | ||
request(options, function(err, response, body){ | ||
if(isRetriableRequest(err, response) && retryOptions.maxAttempts >= 0){ | ||
return setTimeout(tryUntilFail.bind(null, options, f, retryOptions), retryOptions.retryDelay); | ||
Request.prototype._tryUntilFail = function(){ | ||
this.maxAttempts--; | ||
this._req = Request.request(this.options, function(err, response, body){ | ||
if(this._isRetriable(err, response) && this.maxAttempts >= 0){ | ||
this._timeout = setTimeout(this._tryUntilFail.bind(this), this.retryDelay); | ||
return; | ||
} | ||
return f(err, response, body); | ||
}); | ||
} | ||
return this.f(err, response, body); | ||
}.bind(this)); | ||
}; | ||
function RetriableRequest(options, f){ | ||
var retryOptions = _(options || {}).defaults(DEFAULTS).pick(Object.keys(DEFAULTS)).value(); | ||
tryUntilFail(options, f, retryOptions); | ||
Request.prototype._isRetriable = function(err, response){ | ||
// Inspired from https://github.com/geoffreak/request-enhanced/blob/master/src/request-enhanced.coffee#L107 | ||
return (err && _.contains(RETRIABLE_ERRORS, err.code)) || (response && 500 <= response.statusCode && response.statusCode < 600); | ||
}; | ||
Request.prototype.abort = function(){ | ||
if(this._req){ | ||
this._req.abort(); | ||
} | ||
clearTimeout(this._timeout); | ||
this.f(new Error('Aborted')); | ||
}; | ||
function Factory(options, f){ | ||
var retry = _(options || {}).defaults(DEFAULTS).pick(Object.keys(DEFAULTS)).value(); | ||
var req = new Request(options, f, retry.maxAttempts, retry.retryDelay); | ||
req._tryUntilFail(); | ||
return req; | ||
} | ||
module.exports = RetriableRequest; | ||
module.exports = Factory; | ||
Factory.Request = Request; |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "0.0.1", | ||
"version": "1.0.0", | ||
"repository": { | ||
@@ -19,3 +19,7 @@ "url": "https://github.com/FGRibreau/node-request-retry" | ||
"request": "~2.34.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "~1.9.1", | ||
"mocha": "~1.18.2" | ||
} | ||
} |
# Request-retry [](https://david-dm.org/FGRibreau/node-request-retry) | ||
 | ||
[](https://npmjs.org/package/requestretry) | ||
When the connection fails with one of `ECONNRESET`, `ENOTFOUND`, `ESOCKETTIMEDOUT`, `ETIMEDOUT`, `ECONNREFUSED` or when an HTTP 5xx error occurrs, the request will automatically be re-attempted as these are often recoverable errors and will go away on retry. | ||
# Usage | ||
## Usage | ||
@@ -22,3 +22,3 @@ Request-retry is a drop-in replacement for [request](https://github.com/mikeal/request) but adds two new options `maxAttempts` and `retryDelay`. | ||
}, function(err, response, body){ | ||
// this callback will only be called when the request succeeded or after maxAttempts. | ||
// this callback will only be called when the request succeeded or after maxAttempts or on error | ||
}); | ||
@@ -33,6 +33,13 @@ ``` | ||
## Todos | ||
- Tests | ||
- Use an EventEmitter to notify retries | ||
- Allow the end-user to specify its own conditions to trigger a retry | ||
## Changelog | ||
v1.0.0: Initial commit | ||
v1.0.0: request now yield an Request instance with a `.abort()` method. | ||
v0.0.1: Initial commit | ||
Copyright 2014, [Francois-Guillaume Ribreau](http://fgribreau.com) (npm@fgribreau.com) |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5469
6
70
1
44
2