From 3bc85ff31d5c50b3475116a17a4c6347ab242fcb Mon Sep 17 00:00:00 2001 From: Zach Bjornson Date: Sat, 1 Jan 2022 12:30:14 -0800 Subject: [PATCH] WIP: potential fix for #480 --- lib/timeWindowQuantiles.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/timeWindowQuantiles.js b/lib/timeWindowQuantiles.js index 9e7228dd..a67a9381 100644 --- a/lib/timeWindowQuantiles.js +++ b/lib/timeWindowQuantiles.js @@ -43,18 +43,17 @@ class TimeWindowQuantiles { } function rotate() { - let timeSinceLastRotateMillis = Date.now() - this.lastRotateTimestampMillis; - while ( - timeSinceLastRotateMillis > this.durationBetweenRotatesMillis && - this.shouldRotate - ) { - this.ringBuffer[this.currentBuffer] = new TDigest(); - - if (++this.currentBuffer >= this.ringBuffer.length) { - this.currentBuffer = 0; + if (this.shouldRotate) { + let timeSinceLastRotateMillis = Date.now() - this.lastRotateTimestampMillis; + while (timeSinceLastRotateMillis > this.durationBetweenRotatesMillis) { + this.ringBuffer[this.currentBuffer] = new TDigest(); + + if (++this.currentBuffer >= this.ringBuffer.length) { + this.currentBuffer = 0; + } + timeSinceLastRotateMillis -= this.durationBetweenRotatesMillis; + this.lastRotateTimestampMillis += this.durationBetweenRotatesMillis; } - timeSinceLastRotateMillis -= this.durationBetweenRotatesMillis; - this.lastRotateTimestampMillis += this.durationBetweenRotatesMillis; } return this.ringBuffer[this.currentBuffer]; }