ratelimit

Generated by tinox doc

tinox.core.ratelimit, split out of the bundled stdlib as a standalone tinox-central package.

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

maxRequestsInt64
windowMsInt64
requestsList<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

capacityInt64
tokensFloat64
refillRateFloat64
lastRefillInt64

Methods

fn new(capacity: Int64, refillRate: Float64) → TokenBucket
fn allow(bucket: TokenBucket) → Bool
fn refill(bucket: TokenBucket) → Nothing