new RequestCircuitBreaker(options)
A circuit breaker which wraps the request module">www.npmjs.org/package/request}.
All options get passed to the CircuitBreaker (except the request option).
Parameters:
Name | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
options |
object |
Properties
|
- Source:
Extends
Members
-
Promise :module
-
Bluebird Promise module
Type:
- module
- Inherited From:
- Source:
Methods
-
exec() → {external:Promise}
-
Main entry point. Call the protected function and return a promise.
- Inherited From:
- 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.- Inherited From:
- Source:
Returns:
- Type
- integer
-
getCurrentCounts() → {object}
-
Get the current circuit breaker health rolling counts
- Inherited From:
- Source:
Returns:
- Type
- object
-
getCurrentInterval() → {object}
-
Get the current reporting interval.
- Inherited From:
- Source:
Returns:
- Type
- object
-
getCurrentInteval() → {object}
-
- Inherited From:
- Deprecated:
- Yes
- Source:
Returns:
- Type
- object
-
getErrorPercentage() → {float}
-
Get current error percentage.
- Inherited From:
- Source:
Returns:
- Type
- float
-
getName() → {string}
-
Get this instance's name
- Inherited From:
- Source:
Returns:
- Type
- string
-
getQueuedCount() → {integer}
-
Get the amount of requests in queue when concurrency is enabled.
- Inherited From:
- Source:
Returns:
- Type
- integer
-
getState() → {string}
-
Get current circuit's
state
.- Inherited From:
- 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.
- Inherited From:
- Source:
Returns:
- Type
- CircuitBreaker
-
setEmitCallbackEvent(bool) → {CircuitBreaker}
-
Enables/disables emitting the 'callback' event for reporting.
Parameters:
Name Type Description bool
boolean - Inherited From:
- Source:
Returns:
- Type
- CircuitBreaker
-
setEmitIntervalEvent(bool) → {CircuitBreaker}
-
Enables/disables emitting the 'interval' event for reporting.
Parameters:
Name Type Description bool
boolean - Inherited From:
- 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
- Inherited From:
- 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
- Inherited From:
- 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.
- Inherited From:
- 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.
- Inherited From:
- 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 - Inherited From:
- Source:
Returns:
- Type
- CircuitBreaker
-
setTimeout(ms) → {CircuitBreaker}
-
Set the timeout in milliseconds. Set to 0 to disable.
Parameters:
Name Type Description ms
integer - Inherited From:
- 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.
- Inherited From:
- 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 - Inherited From:
- 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 - Inherited From:
- Source:
Returns:
- Type
- CircuitBreaker
-
startEvents()
-
Start events. Events starts automatically upon the first request.
This forces start emitting events before any request is made.- Inherited From:
- Source:
-
stopEvents()
-
Stop events. Clears the pending timeouts.
- Inherited From:
- Source:
Events
-
callback
-
Callback event. Emitted on calls which do not timeout.
Type:
- object
- Inherited From:
- 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
- Inherited From:
- 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