Port of Akka's CircuitBreaker
This is a port of Akka's Circuit Breaker to Node.js. When properly configured it can aid in preventing cascading failures in distributed systems.
Because you have distributed resources and you would prefer not to self-inflict a DOS-style attack while minimizing call latency in the presence of errors. See also the Netflix post on creating resilient systems.
npm install circuit-breaker
Determine the configuration settings. The documentation below is largely copied from the Akka source.
max_failures: The maximum error count to accumulate before the gated function is assumed to have tripped the breaker into the OPEN state. NOTE: An error is indicated by invoking the
callback(e, result)with a "truthy" Error value.
call_timeout_ms: Duration (in MS) that should be used to limit the execution time of a gated function. A function that takes longer than this upper bound is assumed to have failed.
reset_timeout_ms: Duration (in MS) that must expire for a tripped breaker to transition to the HALF-OPEN state. When a breaker enters the HALF-OPEN state, the next call will be attempted, but subsequent calls will fail fast until the results of the allowed function are evaluated. If the allowed function succeeds, the failure count is set to zero and the breaker enters the CLOSED state. If it fails, the breaker enters the OPEN state and the reset timer is restarted.
The circuit-breaker wraps either free functions or logically-related functions defined on a single Object. The wrapped function(s) are aliased by the breaker so existing code transparently benefits from the fail-fast behavior.
For a "standalone" functionvarsetImmediatecallback null null;;var gated_function = circuit_breakernew_circuit_breakersource_function5 /* max_failures */10 /* call_timeout_ms */10 /* reset_timeout_ms */;gated_functionconsole.log"Whee!";;
For a set of semantically related functions attached to an Object (eg, a set of methods that correspond to an RPC-ish HTTP API exposed by a single host) :var;;;;// Wrapping an 'API object' in a circuit breaker// makes all the source functions available on the// circuit-breaker instance. All aliased functions// share the same circuit-breaker instance// so their aggregated behavior contributes to a// single error count.var gated_api_adapter = circuit_breakernew_circuit_breaker5 /* max_failures */10 /* call_timeout_ms */10 /* reset_timeout_ms */;gated_api_adapterdo_it'with some value';
There are two states that the circuit-breaker Errors-out on and interrupts the normal control flow:
- Breaker is in the OPEN state: The breaker has been tripped and all
function calls made while in this state will
fail-fast with a
CircuitBreakerErrorinstance provided to the callback.
- Function timeout: A given call has timed out and the callback is invoked
TimeoutErrorinstance. NOTE: any results (or Errors) returned after a
TimeoutErrorhas been raised will be discarded.
The circuit-breaker depends on (asynchronous-only, CPS-style) functions whose
last argument is a callback of the form:
callback(error, result). In order
to tap the call sequence the circuit-breaker assumes that the last function argument
is a callback function whose inputs can be used to update the breaker state. Once the
circuit-breaker state has been updated with the tapped results, the
values are passed to the original callback function.
Therefore, "circuit-gatable" signatures include:
var ;var ;var ;// turtles...
But, if your function parameters are ordered as in:
You're on your own.
- Allow alternative function signatures
- Use a phi-accrual-detector instead of a simple error count