Limits how often something may happen, with token buckets kept in memory, which a restart empties. Logins, password reset requests and the chat's typing messages use it.
f
takeToken(): boolean
key: string,
limit: RateLimit
Takes one token from key's bucket, or returns false when it is empty. The
caller picks the key, such as typing:<userId>, and keys by user or account
so that more tabs or parallel requests don't buy more tokens.
I
RateLimit
Passed to takeToken by each limited action, which declares its own as a constant.
-
capacity: number
How many tokens a full bucket holds, and so the largest burst.
-
refill: number
Tokens regained per second.
v
rateLimitSweepJob: Job
Drops the buckets untouched for an hour, every minute. Every limit in use
refills within the hour, so such a bucket is full and dropping it changes
nothing. main.ts starts it.
Usage
import * as mod from "api/lib/rate-limit.ts";