Javascript Implementation for Circuit Breaker (Port of Hystrix Circuit Breaker)

Downloads in past


1200.0.59 years ago9 years agoMinified + gzip package size for circuit-breakerjs in KB


Circuit Breaker
Built with Grunt Build Status Test Coverage Code Climate npm version Dependency Status devDependency Status npm downloads NPM
Javascript Implementation for Circuit Breaker (Port of Hystrix Circuit Breaker)

This is a UMD module that can be used as AMD module, native and NodeJS.

Getting Started

Run npm install circuit-breakerjs


A Circuit Breaker is a fail fast mechanism which aids in providing stability and prevents cascading failures in distributed systems.
"The basic idea behind the circuit breaker is very simple.
You wrap a protected function call in a circuit breaker object, which monitors for failures. Once the failures reach a certain threshold, the circuit breaker trips, and all further calls to the circuit breaker return with an error, without the protected call being made at all" - Martin Fowler
An example would be a third-party web service or widget which is out of your control.
The Circuit Breaker can wrap either free functions or logically-related functions defined on a single Object. The wrapped function is invoked by the breaker so existing code transparently benefits from the fail-fast behavior.
This implementation is a port of the Hystrix Circuit Breaker:
Please also see Making the Netflix API More Resilient

Initialization Options


Type: Number Default value: 30000
Optional time window that will be used for state calculations milliseconds
The sliding time window is based on Netflix's Hystrix, and defines the half-open state in which a test request will be made to close the circuit on success.


Type: Number Default value: 10
Optional number of the buckets that the time window will be split to (a bucket is a sliding unit that is added/remove from the time window)


Type: Number Default value: 50
Optional tolerance before opening the circuit percentage


Type: Number Default value: 5
Optional calibration of minimum calls before starting to validate measurements


Type: Number Default value: 0
Optional timeout parameter to apply and time the command


Type: Function Default value: Empty function
Optional handler for circuit open


Type: Function Default value: Empty function
Optional handler for circuit close


run (command, optionalfallback, optionaltimeout)

Will execute the command via the circuit breaker and invoke the optional fallback in case the circuit is opened. fallback is an optional method to invoke in case the circuit is opened. timeout is an optional number of milliseconds to timeout the command based on an internal timer.


Will force the circuit breaker to close.


Will force the circuit breaker to open.


Will reset the circuit breaker to its initial state.


Will return a boolean flag indicator for whether the circuit breaker is opened.


Will return a matrix of calculations for the current information from all existing buckets.


var CircuitBreaker = require("CircuitBreaker").CircuitBreaker;
var circuit = new CircuitBreaker({
    slidingTimeWindow: 5000,
    bucketsNumber: 10,
    tolerance: 50,
    calibration: 5,
    timeout: 0,
    onopen: function() {
        // Do Something when opened
    onclose: function() {
        // Do Something when closed
});, failure, timeout) {
      url: "http://a-web-service-of-some-kind",
      context: document.body

For more example, look at the test directory.