Skip to content

Commit c2b2e32

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 3cbb5a8 commit c2b2e32

2 files changed

Lines changed: 2 additions & 2 deletions

File tree

lib/api/apiUtils/rateLimit/tokenBucket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class WorkerTokenBucket {
2929
this.log = log;
3030

3131
// Token buffer configuration
32-
this.tokens = 0;
3332
this.bufferSize = config.tokenBucketBufferSize || 50; // Max tokens to hold
3433
this.refillThreshold = config.tokenBucketRefillThreshold || 20; // Trigger refill when below this
34+
this.tokens = this.bufferSize; // Start with full buffer for fail-open at startup
3535

3636
// Refill state
3737
this.refillInProgress = false;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ describe('WorkerTokenBucket', () => {
4545

4646
assert.strictEqual(bucket.bucketName, 'test-bucket');
4747
assert.deepStrictEqual(bucket.limitConfig, { limit: 100 });
48-
assert.strictEqual(bucket.tokens, 0);
4948
assert.strictEqual(bucket.bufferSize, 50);
5049
assert.strictEqual(bucket.refillThreshold, 20);
50+
assert.strictEqual(bucket.tokens, 50); // Starts with full buffer for fail-open
5151
assert.strictEqual(bucket.refillInProgress, false);
5252
assert.strictEqual(bucket.refillCount, 0);
5353
});

0 commit comments

Comments
 (0)