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
npm i disyuntor
Usage
const disyuntor = require('disyuntor');
const dnsSafeLookup = disyuntor(dns.lookup, {
timeout: '2s',
maxFailures: 5,
cooldown: '15s',
maxCooldown: '60s',
name: 'dns.lookup',
monitor: (details) => logger.panic({ err: details.err, args: details.args }, 'Error on dns.lookup')
});
Timeouts can be expressed either by strings like '15s' or by milliseconds.
Defaults values are:
timeout
: 2smaxFailures
: 5cooldown
: 15smaxCooldown
: 3 * cooldown
License
Copyright (c) 2015 Auth0, Inc. support@auth0.com (http://auth0.com)