We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 541e22f commit 5832d86Copy full SHA for 5832d86
src/rateLimiters/tokenBucket.ts
@@ -61,9 +61,11 @@ class TokenBucket implements RateLimiter {
61
}
62
63
const bucket: RedisBucket = await JSON.parse(bucketJSON);
64
- // TODO check the timestamp on bucket and update however many tokens are supposed to be in there
65
66
- const timeSinceLastQuery: number = timestamp - bucket.timestamp;
+ const timeSinceLastQueryInSeconds: number = Math.min((timestamp - bucket.timestamp) / 60);
+ const tokensToAdd = timeSinceLastQueryInSeconds * this.refillRate;
67
+ const updatedTokenCount = bucket.tokens + tokensToAdd;
68
+ bucket.tokens = updatedTokenCount > this.capacity ? 10 : updatedTokenCount;
69
70
if (bucket.tokens < tokens) {
71
// reject the request, not enough tokens in bucket
0 commit comments