Reference

API Reference Guide

Map of the builder, providers, limiters, configuration types, decisions, reads, updates, and key lifecycle.

This page is a compact API map. The linked concept pages explain behavior and tradeoffs; generated docs.rs documentation contains exhaustive signatures and constructor lists.

A comparison gate branches into a replacement history stack and an ordered history-preservation path.
Conditional writes change state only after a comparator matches, then replace history or preserve one side of it.

Builder

RateLimiterBuilder builds every provider and limiter. Start from the backend. The type that the caller expects from build() selects the target, and build() returns an Arc of it.

BackendConstructorFeatureAddsTargets
LocalBackendRateLimiterBuilder::local()nonenoneLocalRateLimiterProvider, AbsoluteLocalRateLimiter, SuppressedLocalRateLimiter
RedisBackendRateLimiterBuilder::redis(connection)redis-tokio or redis-smolprefixRedisRateLimiterProvider, AbsoluteRedisRateLimiter, SuppressedRedisRateLimiter
HybridBackendRateLimiterBuilder::hybrid(connection)redis-tokio or redis-smolprefix, sync_interval, allowed_lag, allowed_lag_applies_to_rejectionsHybridRateLimiterProvider, AbsoluteHybridRateLimiter, SuppressedHybridRateLimiter

Each target also has a builder() shortcut with the same arguments as its backend constructor, for example LocalRateLimiterProvider::builder() or AbsoluteHybridRateLimiter::builder(connection). A shortcut fixes the target, so its build() needs no annotation. A target of another backend does not compile. See Backends and targets.

Providers and limiters

BackendKeysCalls
Local&strsynchronous
Redis&RedisKeyasync
Hybrid&RedisKeyasync, except try_inc

Each provider exposes .absolute() and .suppressed(). A directly built limiter runs one strategy only. A limiter with a prefix uses the same Redis keys as the strategy of a provider with the same prefix. See Build one strategy directly.

Shared types

TypeRole
RateLimiterBuilderbuilder struct for every provider and limiter
RateLimiterTargetsealed trait of the types that build() returns; the bound for generic code
RateLimitvalidated rate, normalized per second
WindowSize, BucketSizelive-history duration and grouping precision
HardLimitFactormultiplier from soft target to full suppression
SuppressionFactorCachePeriodsuppression-factor cache duration
RateLimitDecisionadmission result and rejection metadata
SuppressedRateLimitSnapshotsuppressed live read
RateLimitComparatorguard over the current live total
HistoryPreservationnewest/oldest retention policy
ConditionalSetOutcomematch status and before/after totals
RedisKeyvalidated Redis/hybrid namespace or subject key
SyncIntervalhybrid synchronization cadence
AllowedLagmaximum age of hybrid local state for read-only calls

Operations

  • inc(...): establish admission, then record allowed usage.
  • is_allowed(...): absolute admission check without increment.
  • get(...): live total or suppressed snapshot; unknown keys return zero without insertion.
  • get_suppression_factor(...): suppressed pressure; unknown keys return 0.0.
  • set_rate_limit(...): update an existing sticky rate without replacing live history.
  • delete(...): remove one key and return its live absolute or accepted suppressed usage.
  • clear(): remove every key for the called strategy under its configured prefix.
  • set_if(...): comparator-gated history replacement.
  • set_if_preserve_history(...): comparator-gated update retaining newest or oldest history.
  • start_cleanup_loop(), stop_cleanup_loop(): idempotent cleanup controls on providers and on each limiter. Each limiter has its own loop, and the controls of a provider start and stop the loops of its two limiters. A loop ends at once after stop_cleanup_loop() or after the last Arc of its limiter drops.
  • window_size(): the window that the provider or limiter was built with, on providers and on each limiter.
  • Hybrid get_estimate(...): local estimate when state permits, Redis refresh otherwise.
  • Hybrid read-only calls (is_allowed, get_estimate, get_suppression_factor) answer from local state at most AllowedLag old and record no activity. For a key with no stored limit they keep a small local entry, without a Redis key, until cleanup removes it.

The hybrid builder adds sync_interval(SyncInterval), allowed_lag(AllowedLag), and allowed_lag_applies_to_rejections(bool). AllowedLag constructors return TrypemaError::InvalidAllowedLag for zero or an overflowing value.

Reads may lazily evict expired buckets. Matched zero conditional updates remove state. Absolute admission remains best-effort under concurrency.