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

axios-circuit-breaker

v1.0.0

Published

Axios interceptor that implements Circuit Breaker pattern

Downloads

12

Readme

axios-circuit-breaker

Quality Gates

Axios interceptor that implements Circuit Breaker pattern.

How to use?

Applying Circuit Breaker

For each target server that you want to control independently you need to create separate axios instance, and apply axiosCircuitBreaker interceptor to it.

For example, you want to have two circuit breakers, one for requests to test.com another for test2.com.

    const axios = require('axios');
    const { axiosCircuitBreaker, CircuitBreakerOpenError } = require('./index');

    const instanceTest1 =  axios.create({
        url: 'http://test1.com'
    });

    axiosCircuitBreaker(instanceTest1);

    const instanceTest2 =  axios.create({
        url: 'http://test2.com'
    });
    
    axiosCircuitBreaker(instanceTest2);

Configuring Circuit Breaker

In the example below, you can see that our function accepts second parameter with options; In the code snipped you can see default values for it, all of them are optional when you pass them;

    axiosCircuitBreaker(axios, {
        // By default, each Circuit Breaker will have auto-generated numeric id
        // though most of the time it is more convenient to have more meaningful id like "UserService", etc.
        // This ID is used in the logs that CircuitBreaker producing.
        id: "UserService",
        // Defines number of requests that need to fail before
        // Circuit Breaker will open;
        threshold: 50,
        // Defines period of time during which the threshold should be reached
        // to switch Circuit Breaker to OPEN state; 
        thresholdPeriodMs: 5000,
        // Defines number of requests that successfully completed in HALF_OPEN state
        // to switch Circuit Breaker to CLOSED state;
        numRequestsToCloseCircuit: 20,
        // Defines time required to switch Circuit Breaker from OPEN to HALF_OPEN;
        resetPeriodMs: 10000,
        // Defines a strategy that tells whether a request response should be counted
        // as fault; By default counts every request that has status code >= 500 as fault;
        isFault: (error) => error.response?.status >= 500,
        // Defines a logger that enables logging of internal state changes inside Circuit Breaker
        // By default, logger is disabled.
        logger: (message) => console.log(message)
    });

Behavior

Circuit Breaker starts in CLOSED state, that means any request can get to the server.

Circuit Breaker will count failed requests using isFault strategy defined in configuration to determine whether a request is failed.

When number of requests failed during thresholdPeriodMs crosses number defined in threshold option, it will enter OPEN state.

In OPEN state Circuit Breaker will prevent any request from happening and will throw CircuitBreakerOpenError.

After resetPeriodMs Circuit Breaker will enter HALF_OPEN state and will allow numRequestsToCloseCircuit number of requests to reach out to the server. Other requests will be cancelled with CicuitBreakerHalfOpenError. If at least one of requests fail, it will reset back to OPEN state.

When all requests in HALF_OPEN state succeed, it will go back to CLOSED state.