Skip to content

Commit

Permalink
fix: Add status for fallback calls.
Browse files Browse the repository at this point in the history
Fixes: nodeshift#2
`CircuitBreaker` status include the number of times the circuit
was fired (regardless of success or failure), the number of failures,
and the number of fallback executions. Failures reset to 0 every
time there is a successful execution.
  • Loading branch information
lance committed Nov 1, 2016
1 parent 0f6b14d commit fe1eeee
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const readFile = circuitBreaker.promisify(fs.readFile);
const breaker = circuitBreaker(readFile, options);

breaker.fire('./package.json', 'utf-8')
.then((result) => console.log(result.toString()))
.then(console.log)
.catch(console.error);
```

Expand Down
10 changes: 7 additions & 3 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CircuitBreaker {
}

fire () {
this.status.fires++;
const args = Array.prototype.slice.call(arguments);

if (this.open || (this.halfOpen && this[halfOpenAttempted])) {
Expand Down Expand Up @@ -68,6 +69,7 @@ class CircuitBreaker {
function failFast (circuit, args) {
if (circuit[fallbackFunction]) {
return new circuit.Promise((resolve, reject) => {
circuit.status.fallbacks++;
resolve(circuit[fallbackFunction].apply(circuit[fallbackFunction], args));
});
}
Expand All @@ -76,7 +78,7 @@ function failFast (circuit, args) {

function succeed (circuit) {
circuit.status.failures = 0;
circuit.status.fires++;
circuit.status.successes++;
circuit[state] = closed;
}

Expand All @@ -91,8 +93,10 @@ function fail (circuit) {
}

class Status {
constructor (failures, fires) {
this.failures = failures || 0;
constructor () {
this.failures = 0;
this.fallbacks = 0;
this.successes = 0;
this.fires = 0;
}
}
Expand Down
34 changes: 34 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,40 @@ test('Passes arguments to the fallback function', (t) => {
});
});

test('CircuitBreaker status', (t) => {
const breaker = circuitBreaker(passFail, { maxFailures: 1 });
const deepEqual = (t, expected) => (actual) => t.deepEqual(actual, expected);

Fidelity.all([
breaker.fire(10).then(deepEqual(t, 10)),
breaker.fire(20).then(deepEqual(t, 20)),
breaker.fire(30).then(deepEqual(t, 30))
])
.then(() => t.deepEqual(breaker.status.fires, 3))
.catch(t.fail)
.then(() => {
breaker.fire(-10)
.then(t.fail)
.catch((value) => {
t.deepEqual(value, -10);
t.deepEqual(breaker.status.failures, 1);
t.deepEqual(breaker.status.fires, 4);
})
.then(() => {
breaker.fallback(() => 'Fallback called');
breaker.fire(-20)
.then((result) => {
// t.deepEqual(result, 'Fallback called');
t.deepEqual(breaker.status.failures, 1);
t.deepEqual(breaker.status.fires, 5);
t.deepEqual(breaker.status.fallbacks, 1);
})
.catch(t.fail);
})
.then(t.end);
});
});

/**
* Returns a promise that resolves if the parameter
* 'x' evaluates to >= 0. Otherwise the returned promise fails.
Expand Down

0 comments on commit fe1eeee

Please sign in to comment.