Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

back

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

back

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

  • 0.1.5
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

back

build
status

NPM

A simple module to be used for creating exponentially weighted backoff attempts. Used in Primus

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 backoff = {
  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,
};

function retry(err) {
  return back(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 # ' + backoff.attempt +
                ' being made after ' + backoff.timeout + 'ms');
  request();
  }, backoff);
}

function request() {
  http.get('http://localhost:9000', function (res) {
    console.log('Successful Response that will not happen!');
  }).on('error', retry);
}

request();

API

back(callback, backoffOpts);

The back function returns you a function that simply executes the callback after a setTimeout. The timeout is what is based on an exponential backoff of course!

Note:

I am considering switching the backoffOpts and callback as I understand it is an irregular api if enough people want it.

Keywords

FAQs

Package last updated on 28 Oct 2013

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc