Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error code levee circuit open error #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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). More information about the circuitbreaker
pattern can be found in the [akka documentation](http://doc.akka.io/docs/akka/snapshot/common/circuitbreaker.html).
pattern can be found in the [akka documentation](https://doc.akka.io/docs/akka/current/common/circuitbreaker.html).

[![Build Status](https://travis-ci.org/krakenjs/levee.svg)](https://travis-ci.org/krakenjs/levee)

Expand Down Expand Up @@ -39,26 +39,26 @@ function fallback(url, callback) {
callback(null, null, new Buffer('The requested website is not available. Please try again later.'));
}

circuit = Levee.createBreaker(service, options);
circuit.fallback = Levee.createBreaker(fallback, options);
breaker = Levee.createBreaker(service, options);
breaker.fallback = Levee.createBreaker(fallback, options);

circuit.on('timeout', function () {
breaker.on('timeout', function () {
console.log('Request timed out.');
});

circuit.on('failure', function (err) {
breaker.on('failure', function (err) {
console.log('Request failed.', err);
});

circuit.run('http://www.google.com', function (err, req, payload) {
breaker.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);
});

var stats, fbStats;
stats = Levee.createStats(circuit);
fbStats = Levee.createStats(circuit.fallback);
stats = Levee.createStats(breaker);
fbStats = Levee.createStats(breaker.fallback);

// Print stats every 5 seconds.
setInterval(function () {
Expand Down Expand Up @@ -94,7 +94,7 @@ An alternative method for creating Breaker instances with the following argument
```javascript
var Levee = require('levee');

function doStuff(context, callback) {
function fn(context, callback) {
callback(null, 'ok');
}

Expand Down Expand Up @@ -128,10 +128,10 @@ var breaker = Levee.createStats(breaker);

#### Options
##### `timeout`
the amount of time to allow an operation to run before terminating with an error.
the amount of time to allow an operation to run before terminating with an error. In case the command execution exceeds the configured timeout, the run callback will be provided with an error with the `ETIMEDOUT` code.

##### `maxFailures`
the number of failures allowed before the Breaker enters the `open` state.
the number of failures allowed before the Breaker enters the `open` state. Once the breaker enters this state, the run callback will be provided with an error with the `EUNAVAILABLE` code.

##### `resetTimeout`
the amount of time to wait before switch the Breaker from the `open` to `half_open` state to attempt recovery.
Expand All @@ -155,6 +155,7 @@ Executes the wrapped functionality within the circuit breaker functionality with

- `context` - any context to be provided to the implementation.
- `callback` - the callback to be fired upon completion with the signature `function (err, [param1, param2, ...])`
- `err` - any error ocurred during the command execution or a levee error caused either by timeout or an open circuit


## Stats
Expand Down
9 changes: 8 additions & 1 deletion lib/breaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ Breaker.prototype._run = function _run(/*args...n, callback*/) {

if (this.isOpen() || this._pendingClose) {
this.emit('reject');
callback(new Error(this.settings.openErrMsg || 'Command not available.'));

var error = new Error(this.settings.openErrMsg || 'Command not available.');
error.name = 'commandUnavailable';
error.code = 'EUNAVAILABLE';

callback(error);

return;
}

Expand All @@ -92,6 +98,7 @@ Breaker.prototype._run = function _run(/*args...n, callback*/) {
var error = new Error(self.settings.timeoutErrMsg || 'Command timeout.');
error.name = 'commandTimeout';
error.code = 'ETIMEDOUT';

timer = undefined;
self._pendingClose = false;
self.emit('timeout');
Expand Down
4 changes: 4 additions & 0 deletions test/breaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ test('failure', function (t) {
breaker.run('not ok', function (err, data) {
t.ok(err);
t.equal(err.message, 'Command not available.');
t.equal(err.name, 'commandUnavailable');
t.equal(err.code, 'EUNAVAILABLE');
t.notOk(data);
t.ok(breaker.isOpen());
t.end();
Expand Down Expand Up @@ -190,6 +192,8 @@ test('timeout', function (t) {
breaker.run('ok', function (err, data) {
t.ok(err);
t.equal(err.message, 'Command timeout.');
t.equal(err.name, 'commandTimeout');
t.equal(err.code, 'ETIMEDOUT');
t.notOk(data);
t.ok(breaker.isOpen());
t.end();
Expand Down