GithubHelp home page GithubHelp logo

nmjmdr / nizcita_js Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 19 KB

Circuit breaker library for Nodejs async-await

JavaScript 100.00%
circuit-breaker circuit-breaker-library async probe-policies nodejs

nizcita_js's Introduction

A circuit-breaker library for async-await invocations in Nodejs

Nizcita-js is circuit-breaker library for invoking async-awit invocations in NodeJS.

The library is simple and provides flexibility to clients to decide the following aspects of a circuit-breaker:

  1. When the circuit closes
  2. When to probe if the main or the primary invocation is working fine (and the circuit can hence be flipped back to use the main/primary invocation)
To acheive aspect 1, nizcita:
  • Maintains the information about "n" latest failures. The information is maintained in a limited circular buffer and can be enumerated by the client.

  • When setting up the circuit breaker, the client supplies a function. Nizcita invokes this function, when the primary invocation fails to determine if the circuit needs to be flipped to use the alternative invocation.

const numFailures = 5
const holdNLatestFailures = 10
let cb = nz.circuitbreaker(holdNLatestFailures,(failuresInfo)=>{
        return failuresInfo.continousFailureCount >= numFailures
      })

In the above code, after there have been 5 continous failures, the client informs nizcita to flip the circuit.

This determination can also be done by enumerating all the failures.

let cb = nz.circuitbreaker(bufferSize,(failuresInfo)=>{
        let enumerator = failuresInfo.getEnumerator()
        while(true) {
          let failure = enumerator.get()
          if(!failure) {
            break
          }
          // process the failure
        }
        return decision
      })

A failure can be of two types: an exception or a time limit exceeded. The client set the limit on amount of time a primary invocation is expected to take. If the primary invocation take more time than this, nizcita treats it as failure and pushes it to the failures buffer.

The time limit can be set in the following manner:

const numFailures = 5
const timeLimit = 2 * 1e6 // time limit is specified in nanoseconds
const holdNLatestFailures = 10
let cb = nz.circuitbreaker(holdNLatestFailures,(failuresInfo)=>{
        return failuresInfo.continousFailureCount >= numFailures
      })
      .setTimeLimit(timeLimit)

Time limit has to be specified as numerical value in nanoseconds.

Note: Nizcita currently does not support cancellation of the async primary invocation, as JS does not support it (unlike C#).

To acheive aspect 2, nizcita:
  1. The client can set the probe policy. The probe policy is used nizcita to determine if to probe (or a canary call/test call) the primary after the circuit has been flipped.

The probe policy can be set in the following manner:

let probeAfterCalls = 10
let cb = nz.circuitbreaker(1,(failures)=>{
        return true
      })
      .probePolicy((flippedStats)=>{
        return flippedStats.calls >= probeAfterCalls
      })

In the above code the client indicates that the probe should be done after "x" number of calls have been made to the "alternate".

The library comes with a few standard set of probe policies including: coin-flip and probablity-based policy

An example setup and use:

const limitFailuresTo = 5
const timeLimit = 2 * 1e6
let cb = nz.circuitbreaker(100,(failuresInfo)=>{
  return failuresInfo.continousFailureCount > limitFailuresTo
})
.setTimeLimit(timeLimit)
.shouldProbe((flippedStats)=>{
  return (Date.now() - flippedStats.flippedAt) > 5
})

async function getOption() {
  let i = 3

  let primary = async function(){
    return await option1(i)
  }
  let alternate = async function(){
    return await option2(i)
  }
  let result = await cb.invoke(primary,alternate)
  console.log(result)
}

Nizcita means decision or to decide in Sanskrit

nizcita_js's People

Contributors

nmjmdr avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.