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 errorFn option to breaker to filter errors #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion lib/circuit-breaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var CircuitBreaker = module.exports = function (func, options) {
this.timeout = this.options.timeout || 10000; // default 10 second timeout (ms)
this.resetTimeout = this.options.resetTimeout || 60000; // default 1 minute reset timeout (ms)
this.maxFailures = this.options.maxFailures || 5; // default max number of failures to open circuit
this.errorFn = this.options.errorFn || function () { return true; }; // optional function to break based on custom conditions for error object

// Initially circuit is always closed
this.forceClosed();
Expand Down Expand Up @@ -90,7 +91,9 @@ CircuitBreaker.prototype._callbackHandler = function(deferred, timeoutID, startT
self.emit('latency', (new Date() - startTime) );

if(err) {
self.handleFailure(err);
if(self.errorFn(err)) {
self.handleFailure(err);
}
deferred.reject(err);
} else {
self.handleSuccess();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "circuitbreaker",
"version": "0.2.1",
"version": "0.3.0",
"description": "circuit breaker is used to provide stability and prevent cascading failures in distributed systems",
"main": "index.js",
"scripts": {
Expand Down
49 changes: 49 additions & 0 deletions test/circuitbreaker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,53 @@ describe('Circuit Breaker', function(){
});

});

describe('errorFn', function () {
it('should break based on error function parameter', function (done) {
var breakerFn = function(id, callback) {
if (id < 0) {
return callback(id);
} else {
return callback(null, 'data for id ' + id);
}
};

var breaker = new CircuitBreaker(breakerFn, {timeout: 10, maxFailures: 3, resetTimeout: 30, errorFn: function (error) { if (error === -2) { return false; } return true }});
var noop = function () {};

breaker.invoke(-1).fail(noop);
breaker.invoke(-2).fail(noop);
breaker.invoke(-1).fail(noop);
breaker.isClosed().should.be.true;

breaker.invoke(-1).fail(noop);
breaker.isOpen().should.be.true;

done();
});

it('should break the circuit normally without an error function', function (done) {
var breakerFn = function(id, callback) {
if (id < 0) {
return callback(id);
} else {
return callback(null, 'data for id ' + id);
}
};

var breaker = new CircuitBreaker(breakerFn, {timeout: 10, maxFailures: 3, resetTimeout: 30});
var noop = function () {};

breaker.invoke(-1).fail(noop);
breaker.invoke(-2).fail(noop);
breaker.invoke(-1).fail(noop);
breaker.isClosed().should.be.false;

breaker.invoke(-1).fail(noop);
breaker.isOpen().should.be.true;

done();
});

});
});