Concepts

Configuration Types

What each validated configuration type means, which units it accepts, and where it applies.

Trypema uses small validated types instead of unitless numbers. Each constructor names its unit, rejects zero or invalid values, and has an _or_panic form for known constants.

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.

Core types

TypeMeaningAccepted constructor unitsDefault
RateLimittarget usage rateper second, minute, hour, day, week, or 30-day monthnone; supplied to inc and conditional updates
WindowSizehow far back live usage is countedseconds through 30-day months10 seconds
BucketSizeinterval used to merge nearby increments into one history bucketmilliseconds through 30-day months100 milliseconds
HardLimitFactorsuppressed hard-capacity multiplierunitless value at least 1.01.0
SuppressionFactorCachePeriodhow long a computed suppression factor may be reusedmilliseconds through days100 milliseconds
SyncIntervalhybrid local-to-Redis flush cadencemilliseconds through hours10 milliseconds
AllowedLagmaximum age of hybrid local state that answers a read-only callmilliseconds through hours1 second

WindowSize, BucketSize, HardLimitFactor, and SuppressionFactorCachePeriod configure every provider and limiter through RateLimiterBuilder. SyncInterval and AllowedLag are in trypema::hybrid and exist only on the hybrid builder, RateLimiterBuilder::hybrid(connection). Both must be at least 1 millisecond. They provide as_milliseconds(), which returns u64, and as_seconds(), as_minutes(), and as_hours(), which return f64.

Example

use trypema::{
    BucketSize, HardLimitFactor, RateLimit, SuppressionFactorCachePeriod, WindowSize,
};

let rate = RateLimit::per_minute_or_panic(300.0); // equivalent to 5 requests/second
let window = WindowSize::minutes_or_panic(1);
let bucket = BucketSize::milliseconds_or_panic(10);
let hard_factor = HardLimitFactor::new_or_panic(1.5);
let cache = SuppressionFactorCachePeriod::milliseconds_or_panic(100);

Capacity and validation

Absolute capacity is rate per second × window seconds, converted to an integer using truncation. Suppressed hard capacity multiplies that value by HardLimitFactor; its soft target is base capacity.

BucketSize must be less than or equal to WindowSize. Builder validates this relationship even when setters are called in another order. One month always means 30 days.

Fallible constructors such as WindowSize::minutes(1) return Result. Use WindowSize::minutes_or_panic(1) only when value is a trusted constant.

Standard-library Duration

Cleanup timing and rejection waiting time use std::time::Duration, not a Trypema wrapper:

use std::time::Duration;

let cleanup_interval = Duration::from_secs(30);

RateLimitDecision::Rejected::retry_after is a Duration until oldest live bucket expires.