circuit-breaker-9

1.0.0 • Public • Published

circuit-breaker-9

node version NPM version build status Test coverage David deps npm download

This package allows the user to implement their own circuit breakers using triggers. The package comes with two triggers, Timeout and Connections.

中文说明

Install

$ npm i circuit-breaker-9 --save

Usage

'use strict';

const Breaker = require('circuit-breaker-9');

const breaker = new Breaker(Breaker.Timeout(1000), {
  duration: 10000,
  maxHit: 3,
});

const resultA = await breaker.run(async () => { /* do something and return */});
const resultB = await breaker.run(async () => { /* do something and return */},{
  timeout: 500,
  async onBreak(){ /* do something when breaked */},
});

More usage can be found in test.js

API

breaker = new Breaker(trigger, config)

Get a new circuit breaker.

  • trigger: Trigger function, see trigger.
  • config: Circuit breaker Configurationn, seeconfig.
  • breaker: A circuit breaker object.

result = await breaker.run(func, options)

Execute an asynchronous function using a circuit breaker

  • func: async ()=>{},Asynchronous function that you want to execute.
  • options: See options
  • result: When the circuit breaker is not activated, the result comes from func; when the circuit breaker is activated, the result is taken from options.onBreak or config.onBreak.

breaker.break(duration)

Activate circuit breaker

  • duration: Break time, default is config.duration

breaker.restore()

Deactivate the circuit breaker

config

name format default description
duration number 60000 Break time(ms),breaker.run() will call onBreak() directly during the duration
maxHit number 1 The maximum number of triggers, when the number of triggers reaches this value, call breaker.break()
onBreak async ()=>{} () => { throw new Error('Circuit breaker is triggered'); } The break function is called if the circuit is broken when the breaker.run() is called.
onState (isHit, state)=>{} undefined When breaker.run() is not broken, the function is executed, allowing the caller to do some extra processing based on the execution state.

options

name format description
onBreak async ()=>{} See config.onBreak
onState (isHit, state)=>{} See config.onState

trigger

Function for triggering the circuit breaker and modifying the state of the circuit breaker

async function(func, options, state, onState){
  // Execute func and modify state.hit
  return {isHit, err, data}
}
  • func: The func of breaker.run(func, options)
  • options: The options of breaker.run(func, options)
  • state: At least two attributes, hit and breakTime, are included, and other state data in the trigger is also stored here.
  • onState: function(isHit, state),This parameter allows the caller to get the function execution state when there is no open circuit. isHit is triggered this time, state is the state data to be passed. This parameter can be undefined
  • isHit: Whether the trigger is triggered.
  • err: An error object.
  • data: The result of func.

Breaker.Timeout(timeout)

Timeout trigger, allowed to be specified by options.timeout

Breaker.Connection(maxConn)

Maximum connection trigger, triggered when a task that is being processed simultaneously exceeds the maximum connection

Test

npm test

License

MIT
This README was translate by google

Package Sidebar

Install

npm i circuit-breaker-9

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

14.9 kB

Total Files

5

Last publish

Collaborators

  • 985ch