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

stubborn

Package Overview
Dependencies
Maintainers
3
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stubborn

Retry engine


Version published
Weekly downloads
740
decreased by-27.66%
Maintainers
3
Weekly downloads
 
Created
Source

Stubborn

A retry engine

Install

npm install stubborn

Example

var Stubborn = require('stubborn');

var options = {
  maxAttempts: 5,
  delay: 1000
};

var stubborn = new Stubborn(task, options, callback);

stubborn.on('attemptError', onAttemptError);

stubborn.run();

function task(callback) {
  if (Math.random() > 0.2) {
    callback('Task error');
  } else {
    callback(null, 'Task result');
  }
}

function callback(err, result) {
  if (err) {
    console.error(err);
    return;
  }
  console.log(result);
}

function onAttemptError(err) {
  console.error(err);
}

pluggable retry algorithm

All retry algorithms need the current number of attempts as input. As output, they are expected to produce a number that will be used as a factor of the delay.

var Stubborn = require('stubborn');

var options = {
  maxAttempts: 5,
  delay: 1000,
  retryAlgorithm: Stubborn.exponentialBackoff()
};

var stubborn = new Stubborn(task, options, callback);

implement your own:

var options = {
  maxAttempts: 5,
  delay: 1000,
  retryAlgorithm: function(attempts) {
    // delay next execution in options.delay * attempts * 2 
    // thus in attempt #2 we'll have 1000ms * 2 * 2 = 4 seconds delay
    return attempts * 2
  }
};
out of the box algorithms:
var Stubborn = require('stubborn');

var algo1 = Stubborn.exponentialBackoff(2)       // classic http://en.wikipedia.org/wiki/Exponential_backoff
var algo2 = Stubborn.simpleExponentialBackoff(2) // same as the above only without the random element
var algo3 = Stubborn.logarithmicProgression(2)   // logarithmic progression
var algo4 = Stubborn.linear(1, 0)                // ax+b
var algo5 = Stubborm.constant(1)                 // constant / fixed progression
configure using names instead of functions
var options = {
  retryAlgorithm: 'linear',
  retryAlgorithmArgs: [ 1, 0 ]
}

Methods

  • run starts specified task, call it only once
  • cancel stops retries

Events

  • run
  • onAttemptError
  • schedule

Keywords

FAQs

Package last updated on 31 Aug 2018

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