Comparing version 1.0.0 to 1.0.1
@@ -55,4 +55,5 @@ 'use strict'; | ||
Breaker.prototype.run = function run(context, callback) { | ||
var fallback, fn; | ||
var self, fallback, fn; | ||
self = this; | ||
fallback = this.fallback; | ||
@@ -63,3 +64,3 @@ fn = callback; | ||
fn = function wrapper(err/*, ...data*/) { | ||
if (err) { | ||
if (err && self.isOpen()) { | ||
fallback.run(context, callback); | ||
@@ -66,0 +67,0 @@ return; |
{ | ||
"name": "levee", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A circuitbreaker implementation for Node.js", | ||
"main": "index.js", | ||
"directories": { | ||
"example": "examples", | ||
"test": "test" | ||
@@ -17,2 +16,4 @@ }, | ||
"circuit", | ||
"breaker", | ||
"circuitbreaker", | ||
"levee" | ||
@@ -19,0 +20,0 @@ ], |
144
README.md
@@ -5,3 +5,4 @@ Levee | ||
A [circuit breaker](http://doc.akka.io/docs/akka/snapshot/common/circuitbreaker.html) implementation based heavily on | ||
ryanfitz's [node-circuitbreaker](https://github.com/ryanfitz/node-circuitbreaker). | ||
ryanfitz's [node-circuitbreaker](https://github.com/ryanfitz/node-circuitbreaker). More information about the circuitbreaker | ||
pattern can be found in the [akka documentation](http://doc.akka.io/docs/akka/snapshot/common/circuitbreaker.html). | ||
@@ -14,30 +15,5 @@ [![Build Status](https://travis-ci.org/totherik/levee.svg)](https://travis-ci.org/totherik/levee) | ||
var http = require('http'); | ||
var Levee = require('levee'); | ||
var Wreck = require('wreck'); | ||
function service(url, callback) { | ||
var req; | ||
req = http.get(url, function (res) { | ||
var body; | ||
body = []; | ||
res.on('readable', function () { | ||
var chunk; | ||
while ((chunk = res.read()) !== null) { | ||
body.push(chunk); | ||
} | ||
}); | ||
res.on('end', function () { | ||
callback(null, Buffer.concat(body)); | ||
}); | ||
}); | ||
req.on('error', callback); | ||
}; | ||
var options, circuit; | ||
@@ -51,7 +27,7 @@ | ||
circuit = Levee.createBreaker(service, options); | ||
circuit.run('http://www.google.com', function (err, data) { | ||
circuit = Levee.createBreaker(Wreck.get, options); | ||
circuit.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 || data); | ||
console.log(err || payload); | ||
}); | ||
@@ -66,3 +42,3 @@ | ||
function fallback(url, callback) { | ||
callback(null, new Buffer('The requested website is not responding. Please try again later.')); | ||
callback(null, null, new Buffer('The requested website is not available. Please try again later.')); | ||
} | ||
@@ -81,6 +57,6 @@ | ||
circuit.run('http://www.google.com', function (err, data) { | ||
circuit.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 || data); | ||
console.log(err || payload); | ||
}); | ||
@@ -97,2 +73,102 @@ | ||
}, 5000); | ||
``` | ||
``` | ||
## API | ||
### new Breaker(command [, options]) | ||
Creates a new Breaker instance with the following arguments: | ||
- `command` - an object with a property named `execute` with value being a function using the signature: | ||
`function (context, callback)` where: | ||
- `context` - Any context needed to execute the desired behavior. | ||
- `callback` - A callback function with the signature `function (err, [arg1, arg2, ...])` | ||
```javascript | ||
var Levee = require('levee'); | ||
var breaker = new Levee.Breaker({ execute: fn }, options); | ||
``` | ||
### createBreaker(command [, options]) | ||
An alternative method for creating Breaker instances with the following arguments: | ||
- `command` - either function or an object with a property named `execute` with value being a function using the signature: | ||
`function (context, callback)` where: | ||
- `context` - Any context needed to execute the desired behavior. | ||
- `callback` - A callback function with the signature `function (err, [arg1, arg2, ...])` | ||
```javascript | ||
var Levee = require('levee'); | ||
function doStuff(context, callback) { | ||
callback(null, 'ok'); | ||
} | ||
var breaker = Levee.createBreaker(fn, options); | ||
``` | ||
### new Stats(breaker) | ||
Create a new Stats instance with the following argument: | ||
- `breaker` - a Breaker instance | ||
```javascript | ||
var Levee = require('levee'); | ||
var breaker = new Levee.Stats(breaker); | ||
``` | ||
### createStats(breaker) | ||
An alternative method for creating a new Stats instance with the following argument: | ||
- `breaker` - a Breaker instance | ||
```javascript | ||
var Levee = require('levee'); | ||
var 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: | ||
- `context` - any context to be provided to the implementation. | ||
- `callback` - the callback to be fired upon completion with the signature `function (err, [param1, param2, ...])` | ||
## Stats | ||
`new Levee.Stats(breaker)` or `Levee.createStats(breaker)` | ||
#### Methods | ||
##### `increment(name)` | ||
##### `decrement(name)` | ||
##### `sample(name, value)` | ||
##### `snapshot()` | ||
##### `reset()` | ||
##### `resetCounts([name])` | ||
##### `resetSamples([name])` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13175
246
168