Guides

Configuration

Validated settings controlling windows, grouping, suppression, cleanup, and synchronization.

Every provider builder implements RateLimiterBuilder; Redis and hybrid builders add their connection and namespace settings. Start with defaults, then tune only for a measured need.

A paper timeline with request buckets inside a moving rectangular window and one expired bucket outside its left edge.
Only buckets inside the moving window contribute to the live total; older buckets expire as time advances.

Choose settings by behavior

GoalSettingTradeoff
Change enforcement horizonWindowSizelonger windows smooth bursts but retain history longer
Change time precisionBucketSizesmaller buckets improve precision and increase state work
Shape gradual sheddingHardLimitFactorlarger values spread suppression across more traffic
Reduce factor recomputationSuppressionFactorCachePeriodlonger periods can make pressure briefly less current
Bound inactive statestale_after, cleanup_intervalfaster cleanup performs more background work
Balance hybrid lag and Redis loadSyncIntervalshorter intervals reduce lag and increase Redis work
Isolate Redis stateprefixcooperating instances must use the same namespace

See Configuration Types for meanings, defaults, units, and validation. build() also validates relationships such as BucketSize <= WindowSize regardless of setter order.

Example

use std::time::Duration;
use trypema::{
    BucketSize, HardLimitFactor, RateLimiterBuilder, SuppressionFactorCachePeriod, WindowSize,
    local::LocalRateLimiterProvider,
};

let provider = LocalRateLimiterProvider::builder()
    .window_size(WindowSize::minutes_or_panic(1))
    .bucket_size(BucketSize::milliseconds_or_panic(10))
    .hard_limit_factor(HardLimitFactor::new_or_panic(1.5))
    .suppression_factor_cache_period(
        SuppressionFactorCachePeriod::milliseconds_or_panic(100),
    )
    .stale_after(Duration::from_secs(600))
    .cleanup_interval(Duration::from_secs(30))
    .build()
    .unwrap();

Keep settings consistent for Redis-backed instances sharing one namespace.