bbc/levee

Name: levee

Owner: BBC

Description: A circuit-breaker pattern implementation with fallback support.

Created: 2015-09-07 14:09:35.0

Updated: 2015-11-21 12:05:11.0

Pushed: 2015-09-07 14:38:43.0

Homepage: null

Size: 116

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Levee

A circuit breaker implementation based heavily on ryanfitz's node-circuitbreaker. More information about the circuitbreaker pattern can be found in the akka documentation.

Build Status

Basic Usage
 strict';

Levee = require('levee');
Wreck = require('wreck');

options, circuit;

ons = {
maxFailures: 5,
timeout: 60000,
resetTimeout: 30000


uit = Levee.createBreaker(Wreck.get, options);
uit.run('http://www.google.com', function (err, req, payload) {
// If the service fails or timeouts occur 5 consecutive times,
// the breaker opens, fast failing subsequent requests.
console.log(err || payload);

Advanced Usage
tion fallback(url, callback) {
callback(null, null, new Buffer('The requested website is not available. Please try again later.'));


uit = Levee.createBreaker(service, options);
uit.fallback = Levee.createBreaker(fallback, options);

uit.on('timeout', function () {
console.log('Request timed out.');


uit.on('failure', function (err) {
console.log('Request failed.', err);


uit.run('http://www.google.com', function (err, req, payload) {
// If the service fails or timeouts occur 5 consecutive times,
// the breaker opens, fast failing subsequent requests.
console.log(err || payload);


stats, fbStats;
s = Levee.createStats(circuit);
ats = Levee.createStats(circuit.fallback);

rint stats every 5 seconds.
nterval(function () {
console.log(stats.snapshot());
console.log(fbStats.snapshot());
000);
API
new Breaker(command [, options])

Creates a new Breaker instance with the following arguments:

Levee = require('levee');

breaker = new Levee.Breaker({ execute: fn }, options);
createBreaker(command [, options])

An alternative method for creating Breaker instances with the following arguments:

Levee = require('levee');

tion doStuff(context, callback) {
callback(null, 'ok');


breaker = Levee.createBreaker(fn, options);
new Stats(breaker)

Create a new Stats instance with the following argument:

Levee = require('levee');

breaker = new Levee.Stats(breaker);
createStats(breaker)

An alternative method for creating a new Stats instance with the following argument:

Levee = require('levee');

breaker = Levee.createStats(breaker);
Breaker

new Levee.Breaker(command, options) or Levee.createBreaker(command, options)

Options timeout

the amount of time to allow an operation to run before terminating with an error.

maxFailures

the number of failures allowed before the Breaker enters the open state.

resetTimeout

the amount of time to wait before switch the Breaker from the open to half_open state to attempt recovery.

Properties fallback

a Breaker instance to fallback to in the case of the Breaker entering the open state.

Methods run(context, callback)

Executes the wrapped functionality within the circuit breaker functionality with the arguments:

Stats

new Levee.Stats(breaker) or Levee.createStats(breaker)

A simple data aggregation object.

Methods increment(name)

Increment a named counter.

decrement(name)

Decrement a named counter.

sample(name, value)

Take a sample of a given value.

snapshot()

Get the current state of the current Stats instance. Returns an object with the following properties:

reset()

Resets all counts and samples.

resetCounts([name])

Reset counts for the provided name. If no name is provided, resets all counts.

resetSamples([name])

Reset samples for the provided name. If no name is provided, resets all samples.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.