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

back

v1.0.2

Published

Simple exponential backoff pulled out of Primus by @3rd-Eden

Downloads

56,873

Readme

back

build
status

NPM

A simple module to be used for creating exponentially weighted backoff attempts. Originally extracted from Primus.

NOTICE If you were a pre-1.0.0 back user, the API has changed to what is found below. If you do not like this slightly different abstraction and would prefer the former, slightly simpler API, it is still available with require('back/reconnect').

The API change thanks to a contribution from @Raynos makes things simpler as you don't have to manage the copying of the options object yourself in order to handle repeated backoff cases.

Example

var http = require('http');
var back = require('back');
//
// Options to use for backoff
//
// Remark: This object is modified so it should be cloned if you are dealing
// with independent backoff attempts and want to use these values as a base.
//
var options = {
  retries: 3,
  minDelay: 1000, // Defaults to 500ms
  maxDelay: 10000, // Defaults to infinity
  // The following option is shown with its default value but you will most
  // likely never define it as it creates the exponential curve.
  factor: 2,
};

// Where we will store the backoff instance during a particular backoff attempt
var attempt;

function retry(err) {
  var back = attempt || (attempt = new Back(options));
  return back.backoff(function (fail) {
    if (fail) {
      // Oh noez we never reconnect :(
      console.error('Retry failed with ' + err.message);
      process.exit(1);
    }
    //
    // Remark: .attempt and .timeout are added to this object internally
    //
    console.log('Retry attempt # ' + back.settings.attempt +
                ' being made after ' + back.settings.timeout + 'ms');
  request();
  });
}

function request() {
  http.get('http://localhost:9000', function (res) {
    console.log('Successful Response that will not happen!');
    //
    // If we succeeded, we would set the current to null so the next error
    // generates a new instance.
    //
    attempt = null;
  }).on('error', retry);
}

request();

API

var back = new Back(backoffOpts);

The Back constructor function takes your backoff options and saves them as settings in the internal state of the back object.

back.backoff(callback)

The back instance has a backoff method that takes a callback that is executed after a setTimeout. The timeout is what is based on an exponential backoff of course! It will repeatedly all this callback based on the backoff options you passed to the back instance until it exhausts its efforts. When it has exhausted its attempts, it will return an error as the first argument to the callback.

back.close()

Clear backoff timer in cases where you want to dispose of the instance before the callback is executed.