Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/api/apiUtils/rateLimit/tokenBucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class WorkerTokenBucket {
this.log = log;

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

// Refill state
this.refillInProgress = false;
Expand Down
14 changes: 10 additions & 4 deletions tests/unit/api/apiUtils/rateLimit/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@ describe('Rate limit helpers', () => {
const bucketName = 'test-bucket';
const limitConfig = { limit: 100, source: 'bucket' };

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

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

// Token bucket with 0 tokens will log denial
const bucket = tokenBucket.getTokenBucket(bucketName, limitConfig, mockLog);
// Explicitly set tokens to 0 to trigger denial log
bucket.tokens = 0;

helpers.checkRateLimitWithConfig(bucketName, limitConfig, mockLog, () => {
// Check for token bucket denial log
const deniedCall = mockLog.debug.getCalls().find(
Expand Down Expand Up @@ -351,7 +355,9 @@ describe('Rate limit helpers', () => {
const bucketName = 'test-bucket';
const limitConfig = { limit: 100, source: 'bucket' };

// Token bucket starts with 0 tokens
const bucket = tokenBucket.getTokenBucket(bucketName, limitConfig, mockLog);
// Explicitly set tokens to 0 to simulate exhausted quota
bucket.tokens = 0;

helpers.checkRateLimitWithConfig(bucketName, limitConfig, mockLog, (err, rateLimited) => {
assert.strictEqual(rateLimited, true);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/api/apiUtils/rateLimit/tokenBucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ describe('WorkerTokenBucket', () => {

assert.strictEqual(bucket.bucketName, 'test-bucket');
assert.deepStrictEqual(bucket.limitConfig, { limit: 100 });
assert.strictEqual(bucket.tokens, 0);
assert.strictEqual(bucket.bufferSize, 50);
assert.strictEqual(bucket.refillThreshold, 20);
assert.strictEqual(bucket.tokens, 50); // Starts with full buffer for fail-open
assert.strictEqual(bucket.refillInProgress, false);
assert.strictEqual(bucket.refillCount, 0);
});
Expand Down
Loading