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

feat: optional timeout (#200) #202

Merged
merged 1 commit into from
Jun 6, 2018
Merged
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
39 changes: 21 additions & 18 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Please use options.errorThresholdPercentage`;
* There are **no default options** when you use the constructor directly. You
* must supply values for each of these.
* @param options.timeout {Number} The time in milliseconds that action should
* be allowed to execute before timing out.
* be allowed to execute before timing out. Timeout can be disabled by setting
* this to `false`.
* @param options.maxFailures {Number} The number of times the circuit can fail
* before opening.
* (deprecated - see {CircuitBreaker#options.errorThresholdPercentage})
Expand Down Expand Up @@ -315,23 +316,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