diff --git a/src/rateLimiter.ts b/src/rateLimiter.ts index 6c5213b..e27de58 100644 --- a/src/rateLimiter.ts +++ b/src/rateLimiter.ts @@ -64,8 +64,6 @@ export class RateLimiter { /** * Refill tokens based on elapsed time. - * - * BUG: Does not cap at maxTokens, allowing unbounded accumulation. */ refill(): void { const now = Date.now(); @@ -76,8 +74,10 @@ export class RateLimiter { } const intervals = Math.floor(elapsed / this.refillInterval); - // BUG: no cap at maxTokens - this.tokens += intervals * this.refillRate; + const tokensToAdd = intervals * this.refillRate; + + // Cap tokens at maxTokens to prevent unbounded accumulation + this.tokens = Math.min(this.tokens + tokensToAdd, this.maxTokens); this.lastRefillTime += intervals * this.refillInterval; }