auth0/disyuntor

Name: disyuntor

Owner: Auth0

Description: null

Created: 2016-10-24 19:18:41.0

Updated: 2018-04-02 18:48:32.0

Pushed: 2017-05-29 18:41:14.0

Homepage: null

Size: 27

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

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 immdiately 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 succeds 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
i disyuntor
Usage
t disyuntor = require('disyuntor');

t dnsSafeLookup = disyuntor(dns.lookup, {
Timeout for the protected function.
meout: '2s',

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

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

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

This is used in error messages.
me: 'dns.lookup',

optionally log errors
Trip: (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.`)


en use as you will normally use dns.lookup
afeLookup('google.com', (err, ip) => {
 (err) { return console.error(err.message); }
nsole.log(ip);

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

Defaults values are:

Protecting Promise APIs
t lookup = Promise.promisify(require('dns').lookup);

t protectedLookup = disyuntor.promise(lookup, {
me: 'dns.lookup',
meout: '2s',
xFailures: 2


ectedLookup('google.com')
hen((ip)  => console.log(ip),
    (err) => console.error(err));
License

Copyright (c) 2015 Auth0, Inc. support@auth0.com (http://auth0.com)


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.