Providers

Local Provider

In-process rate limiting.

The local provider stores per-key state in memory and is intended for single-process limiting.

Access it through RateLimiter::local().

use trypema::{HardLimitFactor, RateGroupSizeMs, RateLimiter, RateLimiterOptions, SuppressionFactorCacheMs, WindowSizeSeconds};
use trypema::local::LocalRateLimiterOptions;

let rl = RateLimiter::new(RateLimiterOptions {
    local: LocalRateLimiterOptions {
        window_size_seconds: WindowSizeSeconds::try_from(60).unwrap(),
        rate_group_size_ms: RateGroupSizeMs::try_from(10).unwrap(),
        hard_limit_factor: HardLimitFactor::default(),
        suppression_factor_cache_ms: SuppressionFactorCacheMs::default(),
    },
});

let local = rl.local();
let _ = local;

What "local" means

State is stored inside your process, decisions are synchronous (no I/O), state is lost on restart, and multiple instances do not coordinate (each instance enforces its own limit).

Lazy eviction

Trypema evicts expired buckets during normal calls (inc / is_allowed). If a key goes inactive, its state can remain allocated until:

the key is used again or the cleanup loop removes it.

Best practice: enable run_cleanup_loop() if you have many keys, and tune rate_group_size_ms based on the accuracy/performance trade-off you want.

Failure modes

Local rate limiting cannot protect a downstream dependency if your service scales horizontally. If you need a global cap across instances, use the Redis provider.