Retry a request with built-in exponential backoff. |
$ npm install --save retry-request
var request = require('retry-request');
It should work the same as request
in both callback mode and stream mode.
Note: This module only works when used as a readable stream, i.e. POST requests aren't supported (#3).
urlThatReturns503
will be requested 3 total times before giving up and executing the callback.
request(urlThatReturns503, function (err, resp, body) {});
urlThatReturns503
will be requested 3 total times before giving up and emitting the response
and complete
event as usual.
request(urlThatReturns503)
.on('error', function () {})
.on('response', function () {})
.on('complete', function () {});
Passed directly to request
. See the list of options supported: https://github.com/request/request/#requestoptions-callback.
Type: Boolean
Default: false
Set to true
if your custom opts.request
function returns a stream in object mode.
Type: Number
Default: 2
var opts = {
retries: 4
};
request(urlThatReturns503, opts, function (err, resp, body) {
// urlThatReturns503 was requested a total of 5 times
// before giving up and executing this callback.
});
Type: Function
Default: Returns true
if http.incomingMessage.statusCode is < 200 or >= 400.
var opts = {
shouldRetryFn: function (incomingHttpMessage) {
return incomingHttpMessage.statusMessage !== 'OK';
}
};
request(urlThatReturnsNonOKStatusMessage, opts, function (err, resp, body) {
// urlThatReturnsNonOKStatusMessage was requested a
// total of 3 times, each time using `opts.shouldRetryFn`
// to decide if it should continue before giving up and
// executing this callback.
});
Type: Function
Default: request
NOTE: If you override the request function, and it returns a stream in object mode, be sure to set opts.objectMode
to true
.
var originalRequest = require('request').defaults({
pool: {
maxSockets: Infinity
}
});
var opts = {
request: originalRequest
};
request(urlThatReturns503, opts, function (err, resp, body) {
// Your provided `originalRequest` instance was used.
});
Passed directly to request
. See the callback section: https://github.com/request/request/#requestoptions-callback.