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 OPEN
string open No calls are allowed
CLOSED
string closed Calls are allowed
HALF_OPEN
string 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 num
integer 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 bool
boolean - Source:
Returns:
- Type
- CircuitBreaker
-
setEmitIntervalEvent(bool) → {CircuitBreaker}
-
Enables/disables emitting the 'interval' event for reporting.
Parameters:
Name Type Description bool
boolean - 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 name
string Error name
level
float 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 level
float Float from 0 to 1
- Source:
Returns:
- Type
- CircuitBreaker
-
setIntervalSize(ms)
-
Set the reporting interval size.
Parameters:
Name Type Description ms
integer 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 cb
function 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 ms
integer - Source:
Returns:
- Type
- CircuitBreaker
-
setTimeout(ms) → {CircuitBreaker}
-
Set the timeout in milliseconds. Set to 0 to disable.
Parameters:
Name Type Description ms
integer - 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 count
integer 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 count
integer - 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 ms
integer - 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 args
array Array or arguments in exec() call
start
integer Timestamp of the start of the call
end
integer Timestamp of the end of the call
result
array Array of arguments with which callback was called
-
interval
-
Interval event
Type:
- object
- Source:
Properties:
Name Type Description start
integer Timestamp of start of the interval
end
integer Timestamp of the end of the interval
state
string Circuit's current state
active
integer Calls currently running
queued
integer Calls currently queued
total
integer Total calls
success
integer Total success calls
totalErrors
integer Total failed calls
errors
object Error counts by type
Properties
Name Type Description TimeoutError
integer Total calls which timedout - if any
OpenCircuitError
integer Total calls which were rejected (short-circuit) - if any
times
array Times of successful calls in milliseconds