Skip to content

Commit

Permalink
Update token bucket rate limit (#1756)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper authored Dec 9, 2024
1 parent f9a85e0 commit 5c50a0a
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions pages/rate-limit/token-bucket.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Each user has their own bucket of tokens that gets refilled at a set interval. A

## Memory storage

This will only work if memory is persisted across requests. It won't work in serverless environments.
This requires the server to persist its memory across requests and will not work in serverless environments.

```ts
export class TokenBucket<_Key> {
export class TokenBucketRateLimit<_Key> {
public max: number;
public refillIntervalSeconds: number;

Expand All @@ -35,7 +35,7 @@ export class TokenBucket<_Key> {
}
const refill = Math.floor((now - bucket.refilledAt) / (this.refillIntervalSeconds * 1000));
bucket.count = Math.min(bucket.count + refill, this.max);
bucket.refilledAt = now;
bucket.refilledAt = bucket.refilledAt + refill * this.refillIntervalSeconds;
if (bucket.count < cost) {
return false;
}
Expand All @@ -53,9 +53,9 @@ interface Bucket {

```ts
// Bucket that has 10 tokens max and refills at a rate of 2 tokens/sec
const bucket = new TokenBucket<string>(10, 2);
const ratelimit = new TokenBucketRateLimit<string>(10, 2);

if (!bucket.consume(ip, 1)) {
if (!ratelimit.consume(ip, 1)) {
throw new Error("Too many requests");
}
```
Expand Down Expand Up @@ -88,7 +88,7 @@ for i = 1, #fields, 2 do
end
local refill = math.floor((now - refilledAt) / refillIntervalSeconds)
count = math.min(count + refill, max)
refilledAt = now
refilledAt = refilledAt + refill * refillIntervalSeconds
if count < cost then
return {0}
end
Expand All @@ -106,7 +106,7 @@ const SCRIPT_SHA = await client.scriptLoad(script);
Reference the script with the hash.

```ts
export class TokenBucket {
export class TokenBucketRateLimit {
private storageKey: string;

public max: number;
Expand Down Expand Up @@ -136,9 +136,9 @@ export class TokenBucket {
```ts
// Bucket that has 10 tokens max and refills at a rate of 2 tokens/sec.
// Ensure that the storage key is unique.
const bucket = new TokenBucket("global_ip", 10, 2);
const ratelimit = new TokenBucketRateLimit("global_ip", 10, 2);

const valid = await bucket.consume(ip, 1);
const valid = await ratelimit.consume(ip, 1);
if (!valid) {
throw new Error("Too many requests");
}
Expand Down

0 comments on commit 5c50a0a

Please sign in to comment.