mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-12-03 22:12:01 +09:00
Merge pull request #4014 (rudycelekli) into batch
This commit is contained in:
commit
f6314e055d
2 files changed
+14
-3
No files matched your search
@@ -0,0 +1,11 @@
|
||||
import { test } from 'vitest';
|
||||
import assert from 'node:assert/strict';
|
||||
import { SlidingWindowRateLimiter, TokenBucketRateLimiter } from '../v3/@claude-flow/shared/src/resilience/rate-limiter.ts';
|
||||
test('sliding-window admission expires a request at its advertised reset time', () => {
|
||||
const original = Date.now; let now = 1000; Date.now = () => now; const l = new SlidingWindowRateLimiter({ maxRequests: 1, windowMs: 10000 });
|
||||
try { assert.equal(l.consume().allowed, true); now = 11000; assert.equal(l.status().allowed, true); assert.equal(l.consume().allowed, true); } finally { l.destroy(); Date.now = original; }
|
||||
});
|
||||
test('token-bucket retry delay counts only the remaining refill time', () => {
|
||||
const original = Date.now; let now = 1000; Date.now = () => now; const l = new TokenBucketRateLimiter({ maxRequests: 1, windowMs: 10000 });
|
||||
try { l.consume(); now = 10000; for (const r of [l.check(), l.consume()]) { assert.equal(r.allowed, false); assert.equal(r.retryAfter, 1000); assert.equal(r.resetAt.getTime(), now + r.retryAfter); } now = 11000; assert.equal(l.consume().allowed, true); } finally { l.destroy(); Date.now = original; }
|
||||
});
|
||||
@@ -194,7 +194,7 @@ export class SlidingWindowRateLimiter implements RateLimiter {
|
||||
if (!entries) return;
|
||||
|
||||
const cutoff = Date.now() - this.options.windowMs;
|
||||
const filtered = entries.filter((e) => e.timestamp >= cutoff);
|
||||
const filtered = entries.filter((e) => e.timestamp > cutoff);
|
||||
|
||||
if (filtered.length === 0) {
|
||||
this.requests.delete(key);
|
||||
@@ -274,7 +274,7 @@ export class TokenBucketRateLimiter implements RateLimiter {
|
||||
allowed: bucket.tokens >= 1,
|
||||
remaining: Math.floor(bucket.tokens),
|
||||
resetAt: new Date(bucket.lastRefill + this.options.windowMs),
|
||||
retryAfter: bucket.tokens >= 1 ? 0 : this.options.windowMs,
|
||||
retryAfter: bucket.tokens >= 1 ? 0 : Math.max(0, bucket.lastRefill + this.options.windowMs - Date.now()),
|
||||
total: this.options.maxRequests,
|
||||
used: this.options.maxRequests - Math.floor(bucket.tokens),
|
||||
};
|
||||
@@ -292,7 +292,7 @@ export class TokenBucketRateLimiter implements RateLimiter {
|
||||
allowed: false,
|
||||
remaining: 0,
|
||||
resetAt: new Date(bucket.lastRefill + this.options.windowMs),
|
||||
retryAfter: this.options.windowMs,
|
||||
retryAfter: Math.max(0, bucket.lastRefill + this.options.windowMs - Date.now()),
|
||||
total: this.options.maxRequests,
|
||||
used: this.options.maxRequests,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user