Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
retry-request
Advanced tools
The retry-request npm package is designed to make it easier to handle network requests that might need to be retried due to failures or errors. It wraps around the standard request functionality, providing a way to automatically retry requests a specified number of times, with customizable intervals between retries. This is particularly useful in environments where network reliability can't always be guaranteed, or when dealing with services that might occasionally fail or be temporarily unavailable.
Basic retry functionality
This demonstrates how to use retry-request to make a simple HTTP request with retry functionality. If the request fails, retry-request will automatically retry the request based on its default configuration.
const retryRequest = require('retry-request');
const requestOptions = {
url: 'http://example.com'
};
retryRequest(requestOptions, function(err, response, body) {
if (!err) {
console.log(body);
}
});
Custom retry options
This example shows how to customize the retry behavior, including the number of retries, the delay between retries, and even specifying a custom request module to use for the actual HTTP requests.
const retryRequest = require('retry-request');
const requestOptions = {
url: 'http://example.com'
};
const options = {
retries: 5,
request: require('request'),
retryDelay: 1000
};
retryRequest(requestOptions, options, function(err, response, body) {
if (!err) {
console.log(body);
}
});
axios-retry is a package that adds retry functionality to Axios requests. It is similar to retry-request but is specifically designed for use with Axios, a promise-based HTTP client, offering more modern syntax and promise support compared to the callback-based retry-request.
got is a more comprehensive HTTP request library that includes built-in retry functionality among many other features. Unlike retry-request, which is a wrapper around the request library, got is a standalone library offering a wide range of HTTP capabilities, including retries, which makes it a more versatile choice for complex applications.
superagent-retry extends the superagent library with retry capabilities. It is similar to retry-request in that it adds retry functionality to an existing HTTP request library, but it is tailored for superagent users. This package is a good choice for those already using superagent and looking to add simple retry mechanisms.
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.
opts.objectMode
Type: Boolean
Default: false
Set to true
if your custom opts.request
function returns a stream in object mode.
opts.retries
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.
});
opts.shouldRetryFn
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.
});
opts.request
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.
FAQs
Retry a request.
The npm package retry-request receives a total of 5,655,352 weekly downloads. As such, retry-request popularity was classified as popular.
We found that retry-request demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.