Skip to content

Commit

Permalink
feat: optional timeout (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
duartemendes committed May 28, 2018
1 parent 1a61f56 commit fa4e71f
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,23 +315,25 @@ class CircuitBreaker extends EventEmitter {
return new Promise((resolve, reject) => {
const latencyStartTime = Date.now();
if (this.semaphore.test()) {
timeout = setTimeout(
() => {
timeoutError = true;
const error =
new Error(`Timed out after ${this.options.timeout}ms`);
error.code = 'ETIMEDOUT';
/**
* Emitted when the circuit breaker action takes longer than
* `options.timeout`
* @event CircuitBreaker#timeout
*/
const latency = Date.now() - latencyStartTime;
this.semaphore.release();
this.emit('timeout', error, latency);
resolve(handleError(
error, this, timeout, args, latency, resolve, reject));
}, this.options.timeout);
if (this.options.timeout) {
timeout = setTimeout(
() => {
timeoutError = true;
const error =
new Error(`Timed out after ${this.options.timeout}ms`);
error.code = 'ETIMEDOUT';
/**
* Emitted when the circuit breaker action takes longer than
* `options.timeout`
* @event CircuitBreaker#timeout
*/
const latency = Date.now() - latencyStartTime;
this.semaphore.release();
this.emit('timeout', error, latency);
resolve(handleError(
error, this, timeout, args, latency, resolve, reject));
}, this.options.timeout);
}

try {
const result = this.action.apply(this.action, args);
Expand Down

0 comments on commit fa4e71f

Please sign in to comment.