Skip to content

Commit a2682f9

Browse files
committed
refactor: streamline delay handling in FixedWindowCounterLimiter
1 parent 486f11b commit a2682f9

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

packages/core/src/rate-limit/limiters/fixed-window.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313

1414
import { RateLimitExceededError } from "../errors";
15-
import type { RateLimiter, RateLimitExceededAction, RateLimitStats } from "../types";
15+
import type { RateLimitExceededAction, RateLimitStats, RateLimiter } from "../types";
1616

1717
export interface FixedWindowCounterConfig {
1818
/** Maximum requests per window */
@@ -26,7 +26,7 @@ export interface FixedWindowCounterConfig {
2626
}
2727

2828
export class FixedWindowCounterLimiter implements RateLimiter {
29-
private count: number = 0;
29+
private count = 0;
3030
private windowStart: number;
3131
private readonly limit: number;
3232
private readonly windowMs: number;
@@ -63,16 +63,15 @@ export class FixedWindowCounterLimiter implements RateLimiter {
6363
stats,
6464
scope: this.scope,
6565
});
66-
} else {
67-
// Delay until window resets
68-
const waitTime = this.windowStart + this.windowMs - now;
69-
if (waitTime > 0) {
70-
await this.delay(waitTime);
71-
}
72-
// After waiting, reset window and retry
73-
this.count = 0;
74-
this.windowStart = Date.now();
7566
}
67+
// Delay until window resets
68+
const waitTime = this.windowStart + this.windowMs - now;
69+
if (waitTime > 0) {
70+
await this.delay(waitTime);
71+
}
72+
// After waiting, reset window and retry
73+
this.count = 0;
74+
this.windowStart = Date.now();
7675
}
7776

7877
// Increment counter

0 commit comments

Comments
 (0)