Skip to content

Commit 82bb57c

Browse files
committed
CLDSRV-809: Initialize token bucket with full buffer
Workers now start with bufferSize tokens (default 50) instead of 0, enabling fail-open behavior at startup when Redis is unavailable. Previously, new token buckets started empty and required a Redis refill cycle before allowing any requests. This caused all initial requests to be throttled until Redis granted tokens.
1 parent c2b2e32 commit 82bb57c

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

tests/unit/api/apiUtils/rateLimit/helpers.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,9 @@ describe('Rate limit helpers', () => {
231231
const bucketName = 'test-bucket';
232232
const limitConfig = { limit: 100, source: 'bucket' };
233233

234-
// Token bucket starts with 0 tokens (no pre-population)
235-
// This simulates exhausted quota
234+
const bucket = tokenBucket.getTokenBucket(bucketName, limitConfig, mockLog);
235+
// Explicitly set tokens to 0 to simulate exhausted quota
236+
bucket.tokens = 0;
236237

237238
helpers.checkRateLimitWithConfig(bucketName, limitConfig, mockLog, (err, rateLimited) => {
238239
assert.strictEqual(err, null);
@@ -311,7 +312,10 @@ describe('Rate limit helpers', () => {
311312
const bucketName = 'test-bucket';
312313
const limitConfig = { limit: 100, source: 'bucket' };
313314

314-
// Token bucket with 0 tokens will log denial
315+
const bucket = tokenBucket.getTokenBucket(bucketName, limitConfig, mockLog);
316+
// Explicitly set tokens to 0 to trigger denial log
317+
bucket.tokens = 0;
318+
315319
helpers.checkRateLimitWithConfig(bucketName, limitConfig, mockLog, () => {
316320
// Check for token bucket denial log
317321
const deniedCall = mockLog.debug.getCalls().find(
@@ -351,7 +355,9 @@ describe('Rate limit helpers', () => {
351355
const bucketName = 'test-bucket';
352356
const limitConfig = { limit: 100, source: 'bucket' };
353357

354-
// Token bucket starts with 0 tokens
358+
const bucket = tokenBucket.getTokenBucket(bucketName, limitConfig, mockLog);
359+
// Explicitly set tokens to 0 to simulate exhausted quota
360+
bucket.tokens = 0;
355361

356362
helpers.checkRateLimitWithConfig(bucketName, limitConfig, mockLog, (err, rateLimited) => {
357363
assert.strictEqual(rateLimited, true);

0 commit comments

Comments
 (0)