sliding_window
ModuleSliding windows: the current window's count plus the previous window's, weighted by how much of it the sliding window still covers.
Contents
1 submodule, and what the module defines itself.
use utopia_abuse::sliding_window::None;pub struct NoneA store that keeps nothing: every request passes.
use utopia_abuse::sliding_window::Rule;pub struct RuleThe settings of the limit a Store counts for.
use utopia_abuse::SlidingWindow;pub struct SlidingWindow<S>At most limit requests per key in any window_size seconds, estimated
from fixed buckets: a request at 25% into a window counts 75% of the
previous window's requests and all of the current one's.
use utopia_abuse::{SlidingWindow, sliding_window};
let limit = SlidingWindow::new("ip:{ip}", 100, 60, sliding_window::None)?;
let verdict = limit.check_at(1_700_000_000).await?;
assert_eq!((verdict.limit, verdict.reset), (100, 1_700_000_040));use utopia_abuse::sliding_window::Store;pub trait Store: Send + SyncWhere a SlidingWindow keeps its counts: one bucket per key and
window.
sliding_window::redis
Own pageSliding-window buckets in Redis: one string per key and window,
abuse__{<key>}__<window start>. The hash tag keeps a key's buckets in
one cluster slot.
use utopia_abuse::sliding_window::redis::Redis;pub struct Redis<C = ConnectionManager>Buckets in one Redis server, each living ttl seconds.
use utopia_abuse::{Adapter, SlidingWindow, sliding_window};
let conn = redis::Client::open("redis://127.0.0.1:6379")?.get_connection_manager().await?;
let limit = SlidingWindow::new("ip:{ip}", 100, 60, sliding_window::Redis::new(conn, 120))?;
let verdict = limit.with_param("{ip}", "203.0.113.7").check().await?;
println!("{} left", verdict.remaining);Debug.use utopia_abuse::sliding_window::redis::RedisCluster;pub struct RedisClusterBuckets in a Redis Cluster, each living ttl seconds.
Debug.use utopia_abuse::sliding_window::redis::RedisPool;pub struct RedisPool<P>Buckets in Redis or a Redis Cluster, through connections borrowed from a
Pool for each operation.
Clone, Debug.