Skip to content

Commit 44a52f8

Browse files
committed
simplified skipped window functionality
1 parent 101ab24 commit 44a52f8

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

src/rateLimiters/slidingWindowCounter.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,10 @@ class SlidingWindowCounter implements RateLimiter {
115115
if (timestamp >= window.fixedWindowStart + this.windowSize) {
116116
// if more than one window was skipped
117117
if (timestamp >= window.fixedWindowStart + this.windowSize * 2) {
118-
// calculates how many windows may have been skipped since last request
119-
const windowsSkipped = Math.floor(
120-
(timestamp - window.fixedWindowStart) / this.windowSize
121-
);
118+
// if one or more windows was skipped, reset new window to be at current timestamp
122119
updatedUserWindow.previousTokens = 0;
123120
updatedUserWindow.currentTokens = 0;
124-
updatedUserWindow.fixedWindowStart =
125-
window.fixedWindowStart + this.windowSize * windowsSkipped;
121+
updatedUserWindow.fixedWindowStart = timestamp;
126122
} else {
127123
updatedUserWindow.previousTokens = updatedUserWindow.currentTokens;
128124
updatedUserWindow.currentTokens = 0;

0 commit comments

Comments
 (0)