# circuit-breaker-9

> Customizable circuit breakers by triggers

Latest version **1.0.0** (published 2019-07-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install circuit-breaker-9
pnpm add circuit-breaker-9
yarn add circuit-breaker-9
bun add circuit-breaker-9
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2019-07-09 |
| First published | 2019-07-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 1 |
| Unpacked size | 14.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | 985ch |
| Maintainers | 985ch |
| Keywords | circuit, breaker, customizable |

## Links

- npm: https://www.npmjs.com/package/circuit-breaker-9
- Repository: https://github.com/985ch/circuit-breaker-9
- Homepage: https://github.com/985ch/circuit-breaker-9#readme
- Issues: https://github.com/985ch/circuit-breaker-9/issues
- npm.io page: https://npm.io/package/circuit-breaker-9

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.11

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2019-07-09

## README

# circuit-breaker-9

![node version][node-image]
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]

[node-image]: https://img.shields.io/badge/node-%3E%3D8-blue.svg
[npm-image]: https://img.shields.io/npm/v/circuit-breaker-9.svg?style=flat-square
[npm-url]: https://npmjs.org/package/circuit-breaker-9
[travis-image]: https://img.shields.io/travis/985ch/circuit-breaker-9.svg?style=flat-square
[travis-url]: https://travis-ci.org/985ch/circuit-breaker-9
[codecov-image]: https://img.shields.io/codecov/c/github/985ch/circuit-breaker-9.svg?style=flat-square
[codecov-url]: https://codecov.io/github/985ch/circuit-breaker-9?branch=master
[david-image]: https://img.shields.io/david/985ch/circuit-breaker-9.svg?style=flat-square
[david-url]: https://david-dm.org/985ch/circuit-breaker-9
[download-image]: https://img.shields.io/npm/dm/circuit-breaker-9.svg?style=flat-square
[download-url]: https://npmjs.org/package/circuit-breaker-9

This package allows the user to implement their own circuit breakers using triggers. The package comes with two triggers, Timeout and Connections.

## [中文说明](./README.zh_CN.md)
## Install

```bash
$ npm i circuit-breaker-9 --save
```

## Usage
```js
'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](./test.js)

## API
### breaker = new Breaker(trigger, config)
Get a new circuit breaker.
* trigger: Trigger function, see [trigger](#trigger).
* config: Circuit breaker Configurationn, see[config](#config).
* 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](#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
```js
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

```sh
npm test
```

## License

[MIT](LICENSE)<br />
This README was translate by [google](https://translate.google.cn)

---
_Source: https://npm.io/package/circuit-breaker-9 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
