runebook · rune
runebook · rune
No capabilities requested.
manifest: sha256:40d8dd77e9dca8d9e2a471a5d9149f361fd2f102971da141d6fcde546175ef46
rune add @rune/rate-limit@0.1.1A per-key token bucket for commands, API routes, and other requests.
rune add @rune/rate-limit
import { createRateLimiter } from "@rune/rate-limit";
const limiter = createRateLimiter({ capacity: 10, intervalMs: 60_000 });
function checkLimit(accountId: string): Response | undefined {
const decision = limiter.consume(accountId);
if (!decision.allowed) {
return new Response("Too many requests", {
status: 429,
headers: { "Retry-After": String(Math.ceil(decision.retryAfterMs / 1000)) },
});
}
}
Each identity can burst up to capacity. Tokens refill continuously, with
a full refill taking intervalMs. consume(key, cost = 1) returns
allowed, remaining, and retryAfterMs. Set a higher cost for heavier
requests, up to the bucket capacity.
The default budget is 10,000 keys. sweep() discards fully refilled entries.
When the budget is exhausted, new keys are rejected until space frees up.
Keep the limiter alive across requests and use a trusted identity such as
an account ID. Use a shared limiter if replicas need one global quota.