ratelimit
Generated by tinox doc
Two rate-limiting strategies: RateLimiter (sliding window — allow at most N requests per rolling time window) and TokenBucket (continuous refill at a fixed rate, allowing short bursts up to its capacity). Common use: throttling API requests per client.
| Module | Version | Group |
|---|---|---|
mathf | 1.0.0 | tinox.core |
time | 1.0.0 | tinox.core |
Sliding window
import tinox.core.ratelimit;
class Main
{
fnc main() -> Int32
{
// Allow at most 3 requests per 1000ms rolling window.
let limiter: RateLimiter = RateLimiter::new(3, 1000);
var i: Int64 = 0;
while i < 5
{
if RateLimiter::allow(limiter)
{
println("request " + i.toString() + ": allowed");
}
else
{
println("request " + i.toString() + ": rejected (remaining: " + RateLimiter::remaining(limiter).toString() + ")");
}
i = i + 1;
}
return 0;
}
}
Token bucket
import tinox.core.ratelimit;
class Main
{
fnc main() -> Int32
{
// Capacity 5 tokens, refilling at 2 tokens/second — bursts up to
// 5 requests are allowed, then throttled to the refill rate.
let bucket: TokenBucket = TokenBucket::new(5, 2.0);
var i: Int64 = 0;
while i < 7
{
let ok: Bool = TokenBucket::allow(bucket);
println("request " + i.toString() + ": " + ok.toString());
i = i + 1;
}
return 0;
}
}
class RateLimiter
Fields
maxRequests | Int64 | |
windowMs | Int64 | |
requests | List<Int64> |
Methods
fn new(maxRequests: Int64, windowMs: Int64) → RateLimiter
fn allow(limiter: RateLimiter) → Bool
fn reset(limiter: RateLimiter) → Nothing
fn remaining(limiter: RateLimiter) → Int64
class TokenBucket
Fields
capacity | Int64 | |
tokens | Float64 | |
refillRate | Float64 | |
lastRefill | Int64 |
Methods
fn new(capacity: Int64, refillRate: Float64) → TokenBucket
fn allow(bucket: TokenBucket) → Bool
fn refill(bucket: TokenBucket) → Nothing