Skip to content

Commit

Permalink
fix: clear intervals on shutdown (#378)
Browse files Browse the repository at this point in the history
After shutting down a circuit breaker, the status intervals are persisted and continue to run. This keeps track of each interval and shuts them down.
  • Loading branch information
scttcper authored and lance committed Oct 21, 2019
1 parent f6a3e3a commit 91e2dbe
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const WINDOW = Symbol('window');
const BUCKETS = Symbol('buckets');
const TIMEOUT = Symbol('timeout');
const PERCENTILES = Symbol('percentiles');
const BUCKET_INTERVAL = Symbol('bucket-interval');
const SNAPSHOT_INTERVAL = Symbol('snapshot-interval');

const EventEmitter = require('events').EventEmitter;

Expand Down Expand Up @@ -62,20 +64,26 @@ class Status extends EventEmitter {

// rotate the buckets periodically
const bucketInterval = Math.floor(this[TIMEOUT] / this[BUCKETS]);
let interval = setInterval(nextBucket(this[WINDOW]), bucketInterval);
this[BUCKET_INTERVAL] = setInterval(nextBucket(this[WINDOW]),
bucketInterval);

// No unref() in the browser
if (typeof interval.unref === 'function') interval.unref();
if (typeof this[BUCKET_INTERVAL].unref === 'function') {
this[BUCKET_INTERVAL].unref();
}

/**
* Emitted at each time-slice. Listeners for this
* event will receive a cumulative snapshot of the current status window.
* @event Status#snapshot
* @type {Object}
*/
interval = setInterval(_ => this.emit('snapshot', this.stats),
this[SNAPSHOT_INTERVAL] = setInterval(
_ => this.emit('snapshot', this.stats),
bucketInterval);
if (typeof interval.unref === 'function') interval.unref();
if (typeof this[SNAPSHOT_INTERVAL].unref === 'function') {
this[SNAPSHOT_INTERVAL].unref();
}
}

/**
Expand Down Expand Up @@ -154,6 +162,8 @@ class Status extends EventEmitter {

shutdown () {
this.removeAllListeners();
clearInterval(this[BUCKET_INTERVAL]);
clearInterval(this[SNAPSHOT_INTERVAL]);
}
}

Expand Down

0 comments on commit 91e2dbe

Please sign in to comment.