Cleanup
build() starts stale-state cleanup by default for every provider and limiter. Cleanup is
separate from lazy bucket expiration performed by reads and admission operations.

Configure startup
use std::{sync::Arc, time::Duration};
use trypema::RateLimiterBuilder;
use trypema::local::LocalRateLimiterProvider;
let provider: Arc<LocalRateLimiterProvider> = RateLimiterBuilder::local()
.stale_after(Duration::from_secs(600))
.cleanup_interval(Duration::from_secs(30))
.disable_cleanup()
.build()
.unwrap();
stale_after is the inactivity period before a key becomes cleanup-eligible;
cleanup_interval is the time between two passes of a cleanup loop. Their defaults are 10 minutes
and 30 seconds.
disable_cleanup()prevents automatic startup.enable_cleanup()enables automatic startup.cleanup_enabled(bool)supports runtime-conditional configuration.
Control after construction
All providers expose idempotent controls:
provider.start_cleanup_loop();
provider.stop_cleanup_loop();
Each limiter has its own cleanup loop. A provider has no loop of its own: its controls start and stop the loops of its two limiters. To stop the cleanup of one strategy only, stop the loop of that limiter:
provider.absolute().stop_cleanup_loop();
Hybrid synchronization remains mandatory and independent of optional stale-state cleanup.
Directly built limiters
A limiter that build() returns directly runs its own cleanup loop. The cleanup settings above
apply to it too. build() starts the loop unless cleanup is disabled. The limiter has the same
idempotent controls:
use std::sync::Arc;
use trypema::RateLimiterBuilder;
use trypema::local::AbsoluteLocalRateLimiter;
let limiter: Arc<AbsoluteLocalRateLimiter> = RateLimiterBuilder::local()
.disable_cleanup()
.build()
.unwrap();
limiter.start_cleanup_loop();
limiter.stop_cleanup_loop();
The loop ends at once after stop_cleanup_loop() or after the last Arc of the limiter drops. A
pass that runs at that time completes first. A limiter from a provider (absolute() or
suppressed()) also runs its own loop, and the provider starts and stops it.
Hybrid checks
Hybrid read-only calls (is_allowed, get_estimate, get_suppression_factor) record no
activity. A key in accepting state that is only checked becomes stale, and cleanup removes it.
A hybrid check of a key with no stored limit keeps a small local entry, without a Redis key. Only
the cleanup loop removes that entry. With cleanup disabled, call start_cleanup_loop(), or these
entries stay in memory.
What one cleanup pass does
One cleanup pass removes the stale keys of one strategy. The loop of the absolute limiter removes the stale keys of the absolute strategy, and the loop of the suppressed limiter removes those of the suppressed strategy. A provider runs the two loops, so a failed pass of one strategy does not stop the cleanup of the other. The limiter logs the error, and its next pass tries again.
The Redis and hybrid providers remove stale keys from Redis in batches of 500. The first batch
sets the cutoff time of the pass. The pass continues until no key that was stale at that time
stays. Thus, one script call does not block Redis for a long time when many keys become stale
together. The Redis clear() uses the same batches.
A hybrid strategy removes its stale local state after its Redis step succeeds. If the Redis step fails, the local state stays.

