5.0.0 • Published 1 year ago

circuit-fuses v5.0.0

Weekly downloads
93
License
BSD-3-Clause
Repository
github
Last release
1 year ago

Circuit-fuses

Version Downloads Build Status Open Issues License

Wrapper around screwdriver-node-circuitbreaker to define a callback interface

Usage

npm install circuit-fuses

This module wraps the screwdriver-node-circuitbreaker and provides a simple callback interface for handling the circuit breaker.

const Breaker = require('circuit-fuses').breaker;
const request = require('request');
const command = request.get
// To setup the fuse, instantiate a new Breaker with the command to run
const breaker = new Breaker(command);

breaker.runCommand('http://yahoo.com', (err, resp) => {
    if (err) {
        /* If the circuit is open the command is not run, and an error
         * with message "CircuitBreaker Open" is returned.
         * In this case, you can switch on the error and have a fallback technique
         */
        // ... stuff
    }
    // Here there is no error and it's possible to proceed with the resp object
});

The runCommand method will return a promise if a callback is not supplied.

const Breaker = require('circuit-fuses').breaker;
const request = require('request');
const command = request.get
// To setup the fuse, instantiate a new Breaker with the command to run
const breaker = new Breaker(command);

breaker.runCommand('http://yahoo.com')
    .then(resp => {
        // Here there is no error and it's possible to proceed with the resp object
    })
    .catch(err => {
        /* If the circuit is open the command is not run, and an error
         * with message "CircuitBreaker Open" is returned.
         * In this case, you can switch on the error and have a fallback technique
         */
        // ... stuff
    });

Constructor

constructor(command, options) 

ParameterTypeRequiredDescriptionDefault
commandFunctionYesThe command to run with circuit breakernone
options.breakerObjectNoThe object to configure the breaker module with{}
options.breaker.timeoutNumberNoThe timeout in ms to wait for a command10000
options.breaker.maxFailuresNumberNoThe number of failures before the breaker switches5
options.breaker.resetTimeoutNumberNoThe number in ms to wait before resetting the circuit50
options.retryObjectNoThe object to configure the retry module with{}
options.retry.retriesNumberNoThe number of retries to do before passing back a failure5
options.retry.factorNumberNoThe exponential factor to use2
options.retry.minTimeoutNumberNoThe timeout to wait before doing the first retry1000
options.retry.maxTimeoutNumberNoThe max timeout to wait before retryingNumber.MAX_VALUE
options.retry.randomizeBooleanNoRandomize the timeoutfalse
options.shouldRetryFunctionNoSpecial logic to should circuit retries() => true

Short circuiting the retry logic

Normally, the circuit breaker will retry with exponential back off until the max number of retries has been reached or the breaker has opened due to max failures. You may have operations that you want to immediately fail under certain conditions. In this situation, a shouldRetry option is available. This is a function that receives the err object and arguments passed to the runCommand function.

For example, short circuit when runCommand is called with "MyArg" and the error contains a status code of 404:

shouldRetry(err, args) {
    return args[0] === "MyArg" && err.statusCode === 404;
}

Run Command

To run the command passed to the constructor

runCommand(...args, callback)

ParameterTypeRequiredDescription
argsArgumentsNoThe arguments to pass to the command
callbackFunctionNoThe callback to call when the command returns

Get Total Number Requests

Returns the total number of requests from the circuit breaker

getTotalRequests()

Get Total Number Request Timeouts

Returns the total number of request timeouts that occurred

getTimeouts()

Get Total Number Request Successful

Returns the total number of successful requests that occurred

getSuccessfulRequests()

Get Total Number Request Failed

Returns the total number of failed requests that occurred

getFailedRequests()

Get Total Concurrent Requests

Returns the total number of concurrent requests that occurred

getConcurrentRequests()

Get Average Request time

Returns the average request time taken

getAverageRequestTime()

Get whether the breaker is closed

Returns boolean whether the circuit breaker is closed

isClosed()

Get a holistic set of the above metrics

stats()

Using Fuseboxes

A FuseBox is a collection of circuit breakers. If one circuit breaker in the fuse box breaks, the others break as well. To create a new fusebox:

const FuseBox = require('circuit-fuses').box;
const fusebox = new FuseBox();

To add circuit breakers to the fuse box, use the addFuse() method.

addFuse(circuitbreaker)

ParameterTypeRequiredDescription
circuitbreakerCircuitBreakerYesThe circuit breaker to add to the fuse box

Here's an example:

var breaker1 = new Breaker('testFn');
var breaker2 = new Breaker('testFn2');
var breaker3 = new Breaker('testFn3');
fusebox.addFuse(breaker1);
fusebox.addFuse(breaker2);

In the above case, if breaker1 trips, breaker2 will trip as well because both of them belong to the same fuse box.

Testing

npm test

License

Code licensed under the BSD 3-Clause license. See LICENSE file for terms.