ratelimit API reference
A rate limiter over a selectable key/value backend (kvstore.Store - the in-process kv, or memcache / redis through their kvStore). Each key (an IP, a user, an API token) is counted against a limit per time window; a check records a hit and returns a Result with the decision plus the metadata for a compliant 429 (remaining budget, reset time, and Retry-After). Two algorithms, both driven by the wall clock and window-aligned keys (so they work identically on every backend, needing only atomic increment): - fixed window - a counter per aligned window; simplest, but a burst can straddle a window boundary (up to 2x the limit across the seam). - sliding window - a weighted blend of the current and previous window counts, which smooths that boundary burst.
Import with import "ratelimit.j" as ratelimit;. See the ratelimit guide for prose and examples.
Contents
| Name | What it is |
|---|---|
check | Record one hit against key and return the Result (decision + remaining + reset + retry-after). |
fixedWindow | A fixed-window limiter: limit hits per aligned window seconds. |
peek | Report the current state for key without recording a hit: allowed is whether the next hit would be ... |
slidingWindow | A sliding-window limiter: like fixedWindow, but blends the current and previous window counts so a burst ... |
Limiter | struct: A configured limiter: a backend store, a per-window limit, a window in seconds, and the algorithm. |
Result | struct: The outcome of a check / peek. |
Functions
check
ratelimit.check(limiter as Limiter, key as string)Record one hit against key and return the Result (decision + remaining + reset + retry-after). The counter is created and armed with the window TTL on the first hit, so it resets on its own - nothing to reap.
limiter- the configured limiter
key- the counter key (an IP, user, or API token)
- Returns
Result - the decision and rate-limit metadata
fixedWindow
ratelimit.fixedWindow(store as kvstore.Store, limit as int, window as int)A fixed-window limiter: limit hits per aligned window seconds.
store- the backend store
limit- the maximum hits per window
window- the window length in seconds
- Returns
Limiter - the configured limiter
peek
ratelimit.peek(limiter as Limiter, key as string)Report the current state for key without recording a hit: allowed is whether the next hit would be within the limit, plus the same remaining / reset / retry-after metadata.
limiter- the configured limiter
key- the counter key
- Returns
Result - the current rate-limit state
slidingWindow
ratelimit.slidingWindow(store as kvstore.Store, limit as int, window as int)A sliding-window limiter: like fixedWindow, but blends the current and previous window counts so a burst cannot straddle a window boundary.
store- the backend store
limit- the maximum hits per window
window- the window length in seconds
- Returns
Limiter - the configured limiter
Structs
Limiter
A configured limiter: a backend store, a per-window limit, a window in seconds, and the algorithm. Value-semantic; build with fixedWindow / slidingWindow.
def struct Limiter {
store as kvstore.Store,
limit as int,
window as int,
algorithm as string
};store- the backend store
limit- the maximum hits allowed per window
window- the window length in seconds
algorithm- "fixed" or "sliding"
Result
The outcome of a check / peek.
def struct Result {
allowed as bool,
remaining as int,
retryAfter as int,
resetSeconds as int
};allowed- whether this hit is within the limit
remaining- hits left in the window before the limit (0 once exhausted)
retryAfter- seconds to wait before retrying (0 when allowed) - the
Retry-Aftervalue resetSeconds- seconds until the current window rolls over