new CircuitBreaker(fn, fnThis, options)
Create a new circuit breaker wrapping function fn. If the function
is a method of an object, pass the object as the fnThis parameter.
You can configure it's behaviour with the options parameter, or through
various setters:
Parameters:
| Name | Type | Argument | Description | |||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fn |
function | The function to be executed |
||||||||||||||||||||||||||||||||||||||||||||||
fnThis |
object |
<optional> |
If provided, call fn with fnThis as this argument |
|||||||||||||||||||||||||||||||||||||||||||||
options |
object |
<optional> |
Options. All times are in milliseconds. Properties
|
- Source:
Fires:
Example
var cb = new CircuitBreaker(fn)
.setTimeout(1000)
.setErrorThreshold(0.5);
Members
-
<static, readonly> STATE :string
-
State enum
Type:
- string
- Source:
Properties:
Name Type Default Description OPENstring open No calls are allowed
CLOSEDstring closed Calls are allowed
HALF_OPENstring half-open One call is allowed to test health
-
Promise :module
-
Bluebird Promise module
Type:
- module
- Source:
Methods
-
exec() → {external:Promise}
-
Main entry point. Call the protected function and return a promise.
- Source:
Returns:
- Type
- external:Promise
-
getActiveCount() → {integer}
-
Get the amount of current active requests. That is,
calls which have started but have not timedout nor called-back.- Source:
Returns:
- Type
- integer
-
getCurrentCounts() → {object}
-
Get the current circuit breaker health rolling counts
- Source:
Returns:
- Type
- object
-
getCurrentInterval() → {object}
-
Get the current reporting interval.
- Source:
Returns:
- Type
- object
-
getCurrentInteval() → {object}
-
- Deprecated:
- Yes
- Source:
Returns:
- Type
- object
-
getErrorPercentage() → {float}
-
Get current error percentage.
- Source:
Returns:
- Type
- float
-
getName() → {string}
-
Get this instance's name
- Source:
Returns:
- Type
- string
-
getQueuedCount() → {integer}
-
Get the amount of requests in queue when concurrency is enabled.
- Source:
Returns:
- Type
- integer
-
getState() → {string}
-
Get current circuit's
state.- Source:
Returns:
- Type
- string
-
setConcurrency(num) → {CircuitBreaker}
-
Set the concurrency level. Set to 0 to disable (default behaviour).
Requests will be queded if more than this number are currently active.
Parameters:
Name Type Description numinteger The maximum number of requests allowed to be active.
- Source:
Returns:
- Type
- CircuitBreaker
-
setEmitCallbackEvent(bool) → {CircuitBreaker}
-
Enables/disables emitting the 'callback' event for reporting.
Parameters:
Name Type Description boolboolean - Source:
Returns:
- Type
- CircuitBreaker
-
setEmitIntervalEvent(bool) → {CircuitBreaker}
-
Enables/disables emitting the 'interval' event for reporting.
Parameters:
Name Type Description boolboolean - Source:
Returns:
- Type
- CircuitBreaker
-
setErrorNameThreshold(name, level)
-
Set the error threshold for a particular error type.
The error type is obtained from the name property of the error.Parameters:
Name Type Description namestring Error name
levelfloat Float from 0 to 1
- Source:
-
setErrorThreshold(level) → {CircuitBreaker}
-
Set the error threshold which tirggers circuit.
Set to 0 open circuit on first error.Parameters:
Name Type Description levelfloat Float from 0 to 1
- Source:
Returns:
- Type
- CircuitBreaker
-
setIntervalSize(ms)
-
Set the reporting interval size.
Parameters:
Name Type Description msinteger The frequency with which the 'interval' event is emitted.
- Source:
-
setIsErrorHandler(cb) → {CircuitBreaker}
-
Set a custom function to check if the callback was called with an error or not.
Parameters:
Name Type Description cbfunction A function which will be called with the callback arguments.
- Source:
Returns:
- Type
- CircuitBreaker
Example
var cb = new RequestCircuitBreaker(); cb.setIsErrorHandler(function(error, response, body) { if (error) return error; if (response.statusCode == 503) { var unavailableError = new Error(); unavailableError.name = "ServiceUnavailableError"; return unavailableError; } return null; }); -
setResetTime(ms) → {CircuitBreaker}
-
Set the sleep period until a test request is allowed to be made.
The circuit will remain open during this period of time.Parameters:
Name Type Description msinteger - Source:
Returns:
- Type
- CircuitBreaker
-
setTimeout(ms) → {CircuitBreaker}
-
Set the timeout in milliseconds. Set to 0 to disable.
Parameters:
Name Type Description msinteger - Source:
Returns:
- Type
- CircuitBreaker
-
setVolumeThreshold(count) → {CircuitBreaker}
-
Set the minimum amount of calls to start checking for circuit health.
Circuit will always be closed until it reaches this threshold.
Set to 0 to instantly start checking for circuit health.Parameters:
Name Type Description countinteger Minimum amount of calls to start checking circuit health.
- Source:
Returns:
- Type
- CircuitBreaker
-
setWindowCount(count) → {CircuitBreaker}
-
Set the total amount of windows to keep in the rolling counts.
Parameters:
Name Type Description countinteger - Source:
Returns:
- Type
- CircuitBreaker
-
setWindowSize(ms) → {CircuitBreaker}
-
Set the size in milliseconds of each individual window used in the rolling counts.
Parameters:
Name Type Description msinteger - Source:
Returns:
- Type
- CircuitBreaker
-
startEvents()
-
Start events. Events starts automatically upon the first request.
This forces start emitting events before any request is made.- Source:
-
stopEvents()
-
Stop events. Clears the pending timeouts.
- Source:
Events
-
callback
-
Callback event. Emitted on calls which do not timeout.
Type:
- object
- Source:
Properties:
Name Type Description argsarray Array or arguments in exec() call
startinteger Timestamp of the start of the call
endinteger Timestamp of the end of the call
resultarray Array of arguments with which callback was called
-
interval
-
Interval event
Type:
- object
- Source:
Properties:
Name Type Description startinteger Timestamp of start of the interval
endinteger Timestamp of the end of the interval
statestring Circuit's current state
activeinteger Calls currently running
queuedinteger Calls currently queued
totalinteger Total calls
successinteger Total success calls
totalErrorsinteger Total failed calls
errorsobject Error counts by type
Properties
Name Type Description TimeoutErrorinteger Total calls which timedout - if any
OpenCircuitErrorinteger Total calls which were rejected (short-circuit) - if any
timesarray Times of successful calls in milliseconds