Socket
Socket
Sign inDemoInstall

retry-request

Package Overview
Dependencies
29
Maintainers
2
Versions
40
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

retry-request

Retry a request.


Version published
Maintainers
2
Weekly downloads
5,771,071
decreased by-7.3%

Weekly downloads

Package description

What is retry-request?

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.

What are retry-request's main functionalities?

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);
  }
});

Other packages similar to retry-request

Readme

Source
retry-request
Retry a request with built-in exponential backoff.
$ npm install --save teeny-request
$ npm install --save retry-request
var request = require('retry-request', {
  request: require('teeny-request'),
});

It should work the same as request and teeny-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).

Do I need to install request?

Yes! You must independently install teeny-request OR request (deprecated) and provide it to this library:

var request = require('retry-request', {
  request: require('teeny-request'),
});
Callback

urlThatReturns503 will be requested 3 total times before giving up and executing the callback.

request(urlThatReturns503, function (err, resp, body) {});
Stream

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 () {});

Can I monitor what retry-request is doing internally?

Yes! To enable the debug mode, set the environment variable DEBUG to retry-request.

(Thanks for the implementation, @yihaozhadan!)

request(requestOptions, [opts], [cb])

requestOptions

Passed directly to request or teeny-request. See the list of options supported:

opts (optional)

opts.noResponseRetries

Type: Number

Default: 2

The number of times to retry after a response fails to come through, such as a DNS resolution error or a socket hangup.

var opts = {
  noResponseRetries: 0,
};

request(url, opts, function (err, resp, body) {
  // url was requested 1 time before giving up and
  // executing this 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.currentRetryAttempt

Type: Number

Default: 0

var opts = {
  currentRetryAttempt: 1,
};

request(urlThatReturns503, opts, function (err, resp, body) {
  // urlThatReturns503 was requested as if it already failed once.
});
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

If we not provided we will throw an error advising you to provide it.

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('teeny-request').defaults({
  pool: {
    maxSockets: Infinity,
  },
});

var opts = {
  request: originalRequest,
};

request(urlThatReturns503, opts, function (err, resp, body) {
  // Your provided `originalRequest` instance was used.
});
opts.maxRetryDelay

Type: Number

Default: 64

The maximum time to delay in seconds. If retryDelayMultiplier results in a delay greater than maxRetryDelay, retries should delay by maxRetryDelay seconds instead.

opts.retryDelayMultiplier

Type: Number

Default: 2

The multiplier by which to increase the delay time between the completion of failed requests, and the initiation of the subsequent retrying request.

opts.totalTimeout

Type: Number

Default: 600

The length of time to keep retrying in seconds. The last sleep period will be shortened as necessary, so that the last retry runs at deadline (and not considerably beyond it). The total time starting from when the initial request is sent, after which an error will be returned, regardless of the retrying attempts made meanwhile.

cb (optional)

Passed directly to request. See the callback section: https://github.com/request/request/#requestoptions-callback.

Keywords

FAQs

Last updated on 19 Jan 2024

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc