node-breaker

1.0.1 • Public • Published

node-breaker npm version

A configurable implementation of the reset circuit breaker pattern

Installation

npm i node-breaker -S

Usage

const breaker = require('node-breaker')
 
// breaker(functionToWra, [options])
const fn = breaker(functionToWrap, {
  maxFailCount: 5, // number of failures before circuit breaks
  resetTimeout: 10 // seconds before circuit half closes
})
 
// call wrapped function as normal
fn()
 
// wrapped functions always return a promise.
// resolves with whatever was returned from the wrapped function
// or rejects with any errors encountered
fn()
  .then(console.log) // returned data
  .catch(console.error) // error encountered
 
// if function fails more than maxFailCount, circuit
// breaks and will instantly fail calls
fn()
  .catch(console.error) // 'Circuit opened'
 
// after resetTimeout seconds, circuit half closes.
// if no errors encountered, circuit closes again
// otherwise circuit opens and resetTimeout seconds must pass again
fn()
  .then(console.log) // success, circuit closed
  .catch(console.error) // failure, circuit opened again

Readme

Keywords

Package Sidebar

Install

npm i node-breaker

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

2.54 kB

Total Files

3

Last publish

Collaborators

  • renolc