time_limit
ModuleFixed windows: at most limit requests per key in each window of
seconds, the windows aligned on the Unix epoch.
Contents
3 submodules, and what the module defines itself.
use utopia_abuse::time_limit::None;pub struct NoneA store that keeps nothing: every request passes.
use utopia_abuse::time_limit::Rule;pub struct RuleThe settings of the limit a Store counts for.
use utopia_abuse::TimeLimit;pub struct TimeLimit<S>At most limit requests per key in each window of seconds.
A window starts at a multiple of seconds since the Unix epoch, so every
server counts in the same windows. A limit of 0 is off: every request
passes and nothing is stored.
use utopia_abuse::{TimeLimit, time_limit};
let limit = TimeLimit::new("login:{email}", 5, 60, time_limit::None)?.with_param("{email}", "ada@example.com");
// Windows are aligned on minutes: this one resets at the next minute.
let verdict = limit.check_at(1_700_000_017).await?;
assert_eq!(verdict.reset, 1_700_000_040);
assert_eq!(verdict.limit, 5);use utopia_abuse::time_limit::Store;pub trait Store: Send + SyncWhere a TimeLimit keeps its counts: one counter per key and window.
time_limit::database
Own pageFixed-window counters as documents of an abuse collection: one per key
and window, unique on both.
use utopia_abuse::time_limit::database::Database;pub struct Database<D>Counters as documents of the abuse collection: one per key and window,
with its count.
use utopia_abuse::{TimeLimit, time_limit};
let store = time_limit::Database::new(std::sync::Arc::new(time_limit::Memory::new()));
store.setup().await?;
let limit = TimeLimit::new("ip:{ip}", 1, 60, store)?.with_param("{ip}", "203.0.113.7");
assert!(!limit.check_at(1_700_000_000).await?.limited);
assert!(limit.check_at(1_700_000_001).await?.limited);use utopia_abuse::time_limit::database::Entry;pub struct EntryOne counter document of the abuse collection.
use utopia_abuse::time_limit::database::Memory;pub struct MemoryA Documents store in memory, for tests and single-process use. Ids
are sequential ("1", "2", ...).
use utopia_abuse::time_limit::database::Documents;pub trait Documents: Send + SyncThe document operations Database needs, all on the abuse
collection and bypassing authorization.
pub const COLLECTION: &str = "abuse"The collection Database counts in.
time_limit::redis
Own pageFixed-window counters in Redis: one string per key and window,
abuse__<key>__<window start>, expiring with the window.
use utopia_abuse::time_limit::redis::Redis;pub struct Redis<C = ConnectionManager>Counters in one Redis server.
use utopia_abuse::{Adapter, TimeLimit, time_limit};
let conn = redis::Client::open("redis://127.0.0.1:6379")?.get_connection_manager().await?;
let limit = TimeLimit::new("ip:{ip}", 60, 60, time_limit::Redis::new(conn))?.with_param("{ip}", "203.0.113.7");
if limit.check().await?.limited {
println!("slow down");
}Debug.use utopia_abuse::time_limit::redis::RedisCluster;pub struct RedisClusterCounters in a Redis Cluster.
Debug.use utopia_abuse::time_limit::redis::RedisPool;pub struct RedisPool<P>Counters in Redis or a Redis Cluster, through connections borrowed from
a Pool for each operation.
Clone, Debug.pub const NAMESPACE: &str = redis_conn::NAMESPACEThe prefix of every key the Redis stores write.
time_limit::tables
Own pageFixed-window counters as rows of an Appwrite table, through the TablesDB API.
use utopia_abuse::time_limit::tables::Column;pub struct ColumnA column of the Abuse table.
use utopia_abuse::time_limit::tables::Index;pub struct IndexAn index of the Abuse table.
use utopia_abuse::time_limit::tables::Row;pub struct RowOne row of the Abuse table.
use utopia_abuse::time_limit::tables::TablesDb;pub struct TablesDb<T>Counters as rows of the Abuse table of an Appwrite database.
use utopia_abuse::time_limit::tables::Tables;pub trait Tables: Send + SyncThe TablesDB calls TablesDb makes, in database database. Errors are
Error::Appwrite with the API's error type.
pub const COLUMNS: [Column; 3] = [ ColumnThe columns of the Abuse table, sent inline when it is created.
pub const DATABASE_NAME: &str = "Utopia"Undocumented.
pub const INDEXES: [Index; 2] = [ IndexThe indexes of the Abuse table, sent inline when it is created.
pub const TABLE_ID: &str = "Abuse"Undocumented.
pub const TABLE_LOCK: &str = "lock"Undocumented.
pub const TABLE_NAME: &str = "abuse"Undocumented.