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-breakerjs

v0.0.5

Published

Javascript Implementation for Circuit Breaker (Port of Hystrix Circuit Breaker)

Downloads

2,599

Readme

Circuit Breaker

Built with Grunt Build Status Test Coverage Code Climate npm version Dependency Status devDependency Status npm downloads NPM

Javascript Implementation for Circuit Breaker (Port of Hystrix Circuit Breaker)

This is a UMD module that can be used as AMD module, native and NodeJS.

Getting Started

Run npm install circuit-breakerjs

Overview

A Circuit Breaker is a fail fast mechanism which aids in providing stability and prevents cascading failures in distributed systems.

"The basic idea behind the circuit breaker is very simple. You wrap a protected function call in a circuit breaker object, which monitors for failures. Once the failures reach a certain threshold, the circuit breaker trips, and all further calls to the circuit breaker return with an error, without the protected call being made at all" - Martin Fowler

image

An example would be a third-party web service or widget which is out of your control.

The Circuit Breaker can wrap either free functions or logically-related functions defined on a single Object. The wrapped function is invoked by the breaker so existing code transparently benefits from the fail-fast behavior.

This implementation is a port of the Hystrix Circuit Breaker:

image

Please also see Making the Netflix API More Resilient

###Initialization Options

options.slidingTimeWindow

Type: Number Default value: 30000

Optional time window that will be used for state calculations [milliseconds]

The sliding time window is based on Netflix's Hystrix, and defines the half-open state in which a test request will be made to close the circuit on success.

options.bucketsNumber

Type: Number Default value: 10

Optional number of the buckets that the time window will be split to (a bucket is a sliding unit that is added/remove from the time window)

options.tolerance

Type: Number Default value: 50

Optional tolerance before opening the circuit [percentage]

options.calibration

Type: Number Default value: 5

Optional calibration of minimum calls before starting to validate measurements

options.timeout

Type: Number Default value: 0

Optional timeout parameter to apply and time the command

options.onopen

Type: Function Default value: Empty function

Optional handler for circuit open

options.onclose

Type: Function Default value: Empty function

Optional handler for circuit close

API

run (command, [optional]fallback, [optional]timeout)

Will execute the command via the circuit breaker and invoke the optional fallback in case the circuit is opened. fallback is an optional method to invoke in case the circuit is opened. timeout is an optional number of milliseconds to timeout the command based on an internal timer.

close

Will force the circuit breaker to close.

open

Will force the circuit breaker to open.

reset

Will reset the circuit breaker to its initial state.

isOpen

Will return a boolean flag indicator for whether the circuit breaker is opened.

calculate

Will return a matrix of calculations for the current information from all existing buckets.

Example

var CircuitBreaker = require("CircuitBreaker").CircuitBreaker;
var circuit = new CircuitBreaker({
    slidingTimeWindow: 5000,
    bucketsNumber: 10,
    tolerance: 50,
    calibration: 5,
    timeout: 0,
    onopen: function() {
        // Do Something when opened
    },
    onclose: function() {
        // Do Something when closed
    }
});
circuit.run(function(success, failure, timeout) {
    $.ajax({
      url: "http://a-web-service-of-some-kind",
      context: document.body
    }).done(success).fail(failure);
});

For more example, look at the test directory.

License

MIT