Skip to main content

RATE_LIMITER

Constant RATE_LIMITER 

Source
pub const RATE_LIMITER: &str = "// Token-bucket admission control.\n//\n// Not keyed: this limiter governs a single bucket. It previously declared a\n// type parameter `K` that no state, effect, or handler ever referenced, which\n// generated Rust that did not compile (E0392). Per-key limiting is a feature\n// change \u{2014} it needs the key in the state, e.g. `state Available(key: K, ...)`\n// \u{2014} not a bare parameter on the header.\nmachine RateLimiter {\n    state Available(tokens: i64, max_tokens: i64)\n    state Exhausted(retry_after_ms: i64, max_tokens: i64)\n\n    transition acquire: Available -> Available | Exhausted\n    transition refill: Exhausted -> Available\n\n    effect now_ms() -> i64\n\n    on acquire() {\n        if tokens > 0 {\n            goto Available(tokens - 1, max_tokens);\n        } else {\n            goto Exhausted(perform now_ms(), max_tokens);\n        }\n    }\n\n    on refill() {\n        goto Available(max_tokens, max_tokens);\n    }\n}\n";
Expand description

The Gust source for the RateLimiter machine.

A token-bucket rate limiter with two states:

  • Available – tokens remain; requests can proceed.
  • Exhausted – no tokens left; a retry_after_ms value indicates when tokens will be replenished.

Generic over K for the rate-limit key type.