Skip to content

Commit

Permalink
fix(circuit): allow timeout option to be false (#396)
Browse files Browse the repository at this point in the history
* fix(circuit): allow timeout option to be false

Allow `false` to be provided for the `timeout` option as a means of
disabling the entire feature.

Fixes #393.

* test: timeout=false option
  • Loading branch information
mastermatt authored and lance committed Jan 16, 2020
1 parent 1d7b7bd commit 2453326
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Please use options.errorThresholdPercentage`;
* @param {Function} action The action to fire for this {@link CircuitBreaker}
* @param {Object} options Options for the {@link CircuitBreaker}
* @param {Number} options.timeout The time in milliseconds that action should
* be allowed to execute before timing out. Default 10000 (10 seconds)
* be allowed to execute before timing out. Timeout can be disabled by setting
* this to `false`. Default 10000 (10 seconds)
* @param {Number} options.maxFailures (Deprecated) The number of times the
* circuit can fail before opening. Default 10.
* @param {Number} options.resetTimeout The time in milliseconds to wait before
Expand Down Expand Up @@ -112,7 +113,8 @@ class CircuitBreaker extends EventEmitter {
constructor (action, options = {}) {
super();
this.options = options;
this.options.timeout = options.timeout || 10000;
this.options.timeout =
options.timeout === false ? false : options.timeout || 10000;
this.options.resetTimeout = options.resetTimeout || 30000;
this.options.errorThresholdPercentage =
options.errorThresholdPercentage || 50;
Expand Down
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,30 @@ test('CircuitBreaker default capacity', t => {
t.end();
});

test('CircuitBreaker without a timeout', t => {
t.plan(1);

const breaker = new CircuitBreaker(identity, { timeout: false });
const origTimeout = setTimeout;
let spyCalled = false;
// eslint-disable-next-line no-global-assign
setTimeout = () => {
spyCalled = true;
};

t.on('end', () => {
setTimeout = origTimeout; // eslint-disable-line no-global-assign
});

breaker.fire('foo')
.then(() => {
t.equals(spyCalled, false);
breaker.shutdown();
t.end();
})
.catch(t.fail);
});

const noop = _ => {};
const common = require('./common');
const identity = common.identity;
Expand Down

0 comments on commit 2453326

Please sign in to comment.