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

disyuntor

v4.1.1

Published

A circuit-breaker implementation with exponential backoff.

Downloads

1,809

Readme

Build Status

A circuit-breaker implementation for node.js with exponential backoff.

Disyuntor is the Spanish word used for circuit-breaker.

The purpose of this pattern is to detect errors and prevent cascading failures across multiple systems.

Disyuntor wraps an async (errback) function and returns a new function with the same signature.

During normal behavior of the system the circuit remains in its closed state. This means that every call to the wrapper is forwarded to the protected function.

Once the protected function returns more than maxFailures, the breaker trips and every call made during the cooldown interval will immediately return an error preventing resource depletion. This is known as the open state.

Once the system has settled it will allow one call to go to the protected function. If the call succeeds the breaker will be reset to its closed state otherwise it will continue open. This state is known as half open

A call is considered to have failed if the callback is not called before the timeout or if it is called with the first (error) parameter.

Installation

npm i disyuntor

Basic usage

const disyuntor = require('disyuntor');

const dnsSafeLookup = disyuntor.wrapCallbackApi({
  //This is used in error messages.
  name: 'dns.lookup',

  //Timeout for the protected function.
  // timeout: '2s',

  //The number of consecutive failures before switching to open mode
  //and stop calling the underlying service.
  // maxFailures: 5,

  //The minimum time the circuit remains open before doing another attempt.
  // cooldown: '15s',

  //The maximum amount of time the circuit remains open before doing a new attempt.
  // maxCooldown: '60s',

  //optionally log errors
  onTrip: (err, failures, cooldown) => {
    console.log(`dns.lookup triped because it failed ${failures} times.
Last error was ${err.message}! There will be no more attempts for ${cooldown}ms.`);
  },

  onClose: (cooldown) => {
    console.log(`dns.lookup circuit closed after ${cooldown}ms.`;
  }

  // //optional callback to prevent some errors to trigger the disyuntor logic
  // //in this case ENOTFOUND is passed to the callback and will
  // //not trigger the breaker eg:
  // trigger: (err) => err.code !== 'ENOTFOUND'
}, dns.lookup);

//then use as you will normally use dns.lookup
dnsSafeLookup('google.com', (err, ip) => {
  if (err) { return console.error(err.message); }
  console.log(ip);
})

Timeouts can be expressed either by strings like '15s' or by milliseconds.

Defaults values are:

  • timeout: 2s
  • maxFailures: 5
  • cooldown: 15s
  • maxCooldown: 60s

Protecting Promise APIs

const lookup = Promise.promisify(require('dns').lookup);

const protectedLookup = disyuntor.wrapPromise({
  name: 'dns.lookup',
  timeout: '2s',
  maxFailures: 2
}, lookup);

protectedLookup('google.com')
  .then((ip)  => console.log(ip),
        (err) => console.error(err));

Complex scenarios

You can create an instance of Disyuntor to have more control as follows:

const Disyuntor = require('disyuntor').Disyuntor;

const disyuntor = new Disyuntor({
  name: 'dns.lookup',
  timeout: '2s',
  maxFailures: 2
});

await disyuntor.protect(() => dns.lookupAsync('google.com'));

Note: this api only supports promise-returning functions.

License

Copyright (c) 2015 Auth0, Inc. [email protected] (http://auth0.com)