npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

retry-request

v7.0.2

Published

Retry a request.

Downloads

30,393,281

Readme

|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:

  • https://github.com/request/request/#requestoptions-callback
  • https://github.com/googleapis/teeny-request#teenyrequestoptions-callback

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.