
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
easy-breaker
Advanced tools
A simple and low overhead circuit breaker utility.
npm i easy-breaker
Require the library and initialize it with the function you want to put under the Circuit Breaker.
const EasyBreaker = require('easy-breaker')
const simpleGet = require('simple-get')
const get = EasyBreaker(simpleGet)
get('http://example.com', function (err, res) {
if (err) throw err
console.log(res.statusCode)
})
If the function times out, the error will be a TimeoutError
.
If the threshold has been reached and the circuit is open the error will be a CircuitOpenError
.
You can access the errors constructors with require('easy-breaker').errors
.
You can access the state constants with require('easy-breaker').states
.
You can pass some custom option to change the default behavior of EasyBreaker
:
const EasyBreaker = require('easy-breaker')
const simpleGet = require('simple-get')
// the following options object contains the default values
const get = EasyBreaker(simpleGet, {
threshold: 5
timeout: 1000 * 10
resetTimeout: 1000 * 10
context: null,
maxEventListeners: 100
promise: false
})
threshold
: is the maximum numbers of failures you accept to have before opening the circuit.timeout:
is the maximum number of milliseconds you can wait before return a TimeoutError
(read the caveats section about how the timeout is handled).resetTimeout
: time before the circuit will move from open
to half-open
context
: a custom context for the function to callmaxEventListeners
: since this library relies on events, it can happen that you reach the maximum number of events listeners before the memory leak warn. To avoid that log, just set an higher number with this property.promise
: if you need to handle promised API, see below.Promises and async-await are supported as well!
Just pass the option { promise: true }
and you are done!
Note the if you use the promise version of the api also the function you are wrapping should return a promise.
const EasyBreaker = require('easy-breaker')
const got = require('got')
const get = EasyBreaker(got, { promise: true })
get('http://example.com')
.then(console.log)
.catch(console.log)
This circuit breaker is an event emitter, if needed you can listen to its events:
open
half-open
close
result
tick
Run a timer for every function is pretty expensive, especially if you are running the code in a heavy load environment.
To fix this problem and get better performances, EasyBreaker
uses an atomic clock, in other words uses an interval that emits a tick
event every timeout / 2
milliseconds.
Every running functions listens for that event and if the number of ticks received is higher than 3
it will return a TimeoutError
.
Image curtesy of Martin Fowler.
Copyright © 2018 Tomas Della Vedova
FAQs
A simple circuit breaker utility
We found that easy-breaker demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.