Learn to Code ยท zero-dependency ยท MIT

larzratelimit

Token-bucket rate limiting.

Without limits, a traffic burst or one bad client can overwhelm your app or blow a paid-API quota. larzratelimit implements the token-bucket algorithm - allowing bursts while capping the sustained rate - with a per-key limiter to throttle by user or IP.

larzscript pkg install ratelimit
View on GitHub →Learn the stack

What you get

Try it

import "ratelimit" as ratelimit
let buckets = {}
for i in range(12) {
  let ok = ratelimit.allow(buckets, "user:42", 10, 1.0, 0.0)
  print("request", i + 1, "->", ok ? "allowed" : "rate limited")
}
Output
request 1 -> allowed
request 2 -> allowed
request 3 -> allowed
request 4 -> allowed
request 5 -> allowed
request 6 -> allowed
request 7 -> allowed
request 8 -> allowed
request 9 -> allowed
request 10 -> allowed
request 11 -> rate limited
request 12 -> rate limited

Why it's in the Larz Stack

larzratelimit is a real Larzscript package (larzscript pkg install ratelimit) - money-native, zero dependencies, the same language as the rest of the estate, not a separate Python runtime. It composes with the rest of the Larz Stack and the Larz framework.