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-breaker-9

v1.0.0

Published

Customizable circuit breakers by triggers

Downloads

2

Readme

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