Skip to content

Commit

Permalink
feat: add methods to open and close the circuit manually
Browse files Browse the repository at this point in the history
  • Loading branch information
lance committed Nov 1, 2016
1 parent 47b8a20 commit 9c78ecf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ class CircuitBreaker extends EventEmitter {
function _fail (circuit) {
return () => {
circuit.status.failures++;
if (circuit.status.failures >= circuit.options.maxFailures &&
circuit[STATE] !== OPEN) {
circuit[STATE] = OPEN;
circuit.emit('open');
}
circuit.open();
};
}

Expand All @@ -60,11 +56,19 @@ class CircuitBreaker extends EventEmitter {
}
}

open () {
if (this.status.failures >= this.options.maxFailures &&
this[STATE] !== OPEN) {
this[STATE] = OPEN;
this.emit('open');
}
}

get closed () {
return this[STATE] === CLOSED;
}

get open () {
get opened () {
return this[STATE] === OPEN;
}

Expand All @@ -81,7 +85,7 @@ class CircuitBreaker extends EventEmitter {
this.emit('fire');
const args = Array.prototype.slice.call(arguments);

if (this.open || (this.halfOpen && this[PENDING_CLOSE])) {
if (this.opened || (this.halfOpen && this[PENDING_CLOSE])) {
this.emit('reject');
return failFast(this, 'Breaker is open', args);
}
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('api', (t) => {
t.ok(circuitBreaker.promisify);
t.ok(breaker);
t.ok(breaker.fire);
t.notOk(breaker.open);
t.notOk(breaker.opened);
t.notOk(breaker.halfOpen);
t.ok(breaker.closed);
t.ok(breaker.status);
Expand Down

0 comments on commit 9c78ecf

Please sign in to comment.