Rust

time_limit

Module

Fixed 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;
Rust
pub struct None

A store that keeps nothing: every request passes.

use utopia_abuse::time_limit::Rule;
Rust
pub struct Rule

The settings of the limit a Store counts for.

use utopia_abuse::TimeLimit;
Rust
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.

Rust
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;
Rust
pub trait Store: Send + Sync

Where a TimeLimit keeps its counts: one counter per key and window.

time_limit::database

Own page

Fixed-window counters as documents of an abuse collection: one per key and window, unique on both.

use utopia_abuse::time_limit::database::Database;
Rust
pub struct Database<D>

Counters as documents of the abuse collection: one per key and window, with its count.

Rust
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;
Rust
pub struct Entry

One counter document of the abuse collection.

use utopia_abuse::time_limit::database::Memory;
Rust
pub struct Memory

A Documents store in memory, for tests and single-process use. Ids are sequential ("1", "2", ...).

use utopia_abuse::time_limit::database::Documents;
Rust
pub trait Documents: Send + Sync

The document operations Database needs, all on the abuse collection and bypassing authorization.

Implemented byArcMemory
Rust
pub const COLLECTION: &str = "abuse"

The collection Database counts in.

time_limit::redis

Own page

Fixed-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;
Rust
pub struct Redis<C = ConnectionManager>

Counters in one Redis server.

Rust
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");
}
Compiles in CI; needs a running service to execute.
Also implements Debug.
use utopia_abuse::time_limit::redis::RedisCluster;
Rust
pub struct RedisCluster

Counters in a Redis Cluster.

Also implements Debug.
use utopia_abuse::time_limit::redis::RedisPool;
Rust
pub struct RedisPool<P>

Counters in Redis or a Redis Cluster, through connections borrowed from a Pool for each operation.

Also implements Clone, Debug.
Rust
pub const NAMESPACE: &str = redis_conn::NAMESPACE

The prefix of every key the Redis stores write.

time_limit::tables

Own page

Fixed-window counters as rows of an Appwrite table, through the TablesDB API.

use utopia_abuse::time_limit::tables::Column;
Rust
pub struct Column

A column of the Abuse table.

use utopia_abuse::time_limit::tables::Index;
Rust
pub struct Index

An index of the Abuse table.

use utopia_abuse::time_limit::tables::Row;
Rust
pub struct Row

One row of the Abuse table.

use utopia_abuse::time_limit::tables::TablesDb;
Rust
pub struct TablesDb<T>

Counters as rows of the Abuse table of an Appwrite database.

use utopia_abuse::time_limit::tables::Tables;
Rust
pub trait Tables: Send + Sync

The TablesDB calls TablesDb makes, in database database. Errors are Error::Appwrite with the API's error type.

Rust
pub const COLUMNS: [Column; 3] = [ Column

The columns of the Abuse table, sent inline when it is created.

Rust
pub const DATABASE_NAME: &str = "Utopia"

Undocumented.

Rust
pub const INDEXES: [Index; 2] = [ Index

The indexes of the Abuse table, sent inline when it is created.

Rust
pub const TABLE_ID: &str = "Abuse"

Undocumented.

Rust
pub const TABLE_LOCK: &str = "lock"

Undocumented.

Rust
pub const TABLE_NAME: &str = "abuse"

Undocumented.