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: add option to disable stats snapshots #799

Merged
merged 3 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class CircuitBreaker extends EventEmitter {
this.options.cacheTTL = options.cacheTTL ?? 0;
this.options.cacheGetKey = options.cacheGetKey ??
((...args) => JSON.stringify(args));
this.options.enableSnapshots = options.enableSnapshots !== false;

// Set default cache transport if not provided
if (this.options.cache) {
Expand Down
19 changes: 13 additions & 6 deletions lib/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class Status extends EventEmitter {
this.rollingPercentilesEnabled =
options.rollingPercentilesEnabled !== false;

// Default this value to true
this.enableSnapshots = options.enableSnapshots !== false;

// prime the window with buckets
for (let i = 0; i < this[BUCKETS]; i++) this[WINDOW][i] = bucket();

Expand All @@ -80,11 +83,13 @@ class Status extends EventEmitter {
* @event Status#snapshot
* @type {Object}
*/
this[SNAPSHOT_INTERVAL] = setInterval(
_ => this.emit('snapshot', this.stats),
bucketInterval);
if (typeof this[SNAPSHOT_INTERVAL].unref === 'function') {
this[SNAPSHOT_INTERVAL].unref();
if (this.enableSnapshots) {
this[SNAPSHOT_INTERVAL] = setInterval(
_ => this.emit('snapshot', this.stats),
bucketInterval);
if (typeof this[SNAPSHOT_INTERVAL].unref === 'function') {
this[SNAPSHOT_INTERVAL].unref();
}
}

if (options.stats) {
Expand Down Expand Up @@ -169,7 +174,9 @@ class Status extends EventEmitter {
shutdown () {
this.removeAllListeners();
clearInterval(this[BUCKET_INTERVAL]);
clearInterval(this[SNAPSHOT_INTERVAL]);
if (this.enableSnapshots) {
clearInterval(this[SNAPSHOT_INTERVAL]);
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions test/status-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,40 @@ test('CircuitBreaker status - import stats,but not a status object', t => {
t.fail();
}
});

test('CircuitBreaker status - enableSnapshots defaults to true', t => {
t.plan(1);

const breaker = new CircuitBreaker(passFail);

t.equal(breaker.status.enableSnapshots, true, 'enableSnapshots defaults to true');

breaker.shutdown();
t.end();
});

test('CircuitBreaker status - enableSnapshots is true in Status when set to true', t => {
t.plan(1);

const breaker = new CircuitBreaker(passFail, {
enableSnapshots: true
});

t.equal(breaker.status.enableSnapshots, true, 'enableSnapshots propagates as true');

breaker.shutdown();
t.end();
});

test('CircuitBreaker status - enableSnapshots is false in Status when set to false', t => {
t.plan(1);

const breaker = new CircuitBreaker(passFail, {
enableSnapshots: false
});

t.equal(breaker.status.enableSnapshots, false, 'enableSnapshots propagates as false');

breaker.shutdown();
t.end();
});