npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

circuit-fuses

v5.0.0

Published

Wrapper around node-circuitbreaker to define a callback interface

Downloads

355

Readme

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) 

| Parameter | Type | Required | Description | Default | | :------------- | :---- | :---- | :-------------| :---------- | | command | Function | Yes | The command to run with circuit breaker | none | | options.breaker | Object | No | The object to configure the breaker module with | {} | | options.breaker.timeout | Number | No | The timeout in ms to wait for a command | 10000 | | options.breaker.maxFailures | Number | No | The number of failures before the breaker switches | 5 | | options.breaker.resetTimeout | Number | No | The number in ms to wait before resetting the circuit | 50 | | options.retry | Object | No | The object to configure the retry module with | {} | | options.retry.retries | Number | No | The number of retries to do before passing back a failure | 5 | | options.retry.factor | Number | No | The exponential factor to use | 2 | | options.retry.minTimeout | Number | No | The timeout to wait before doing the first retry | 1000 | | options.retry.maxTimeout | Number | No | The max timeout to wait before retrying | Number.MAX_VALUE | | options.retry.randomize | Boolean | No | Randomize the timeout | false | | options.shouldRetry | Function | No | Special 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)

| Parameter | Type | Required | Description | | :------------- | :---- | :---- | :-------------| | args | Arguments | No | The arguments to pass to the command | | callback | Function | No | The 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)

| Parameter | Type | Required | Description | | :------------- | :---- | :---- | :-------------| | circuitbreaker | CircuitBreaker | Yes | The 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.