Rust

Abuse

Rate limits: fixed windows, sliding windows and token buckets over Redis, Redis Cluster, pooled connections, document stores and Appwrite tables, and reCAPTCHA verification.
Rust 3,480 lines (2,101 code)split
PartCodeCommentsBlankTotalFiles
Rust source2,1019654143,48013

Comments include doc comments. Rust tests are tests/ and #[cfg(test)] modules.

Install#

The crate lives in the Appwrite repository; depend on it from Git.

Cargo.toml
[dependencies]
utopia-abuse = { git = "https://github.com/appwrite/appwrite" }

Getting started#

Common tasks with Abuse, each a complete program you can copy and run.

A rate limit caps how often a client may do something: sign in, send a message, call an endpoint. This library counts requests per key (an IP, a user, an endpoint, or all three) and says, for each one, whether it is over the limit, how many requests are left and when the count resets. It has three ways of counting: fixed windows (TimeLimit), sliding windows (SlidingWindow) and token buckets (TokenBucket), each over a store: Redis, a Redis Cluster, a pool of connections, a document store, or nothing at all. Stores are async, so the examples run them on a Tokio runtime. This guide covers the common tasks.

Limit requests per window

A TimeLimit allows limit requests in each window of seconds, windows aligned on the Unix epoch so every server counts in the same ones. check counts a request and says whether it is over the limit; once the window is full, every request is limited until the next one. check_at takes the time instead of reading the clock.

Rust
use utopia_abuse::{TimeLimit, time_limit};

tokio::runtime::Builder::new_current_thread().build()?.block_on(async {
    let store = time_limit::Database::new(time_limit::Memory::new());
    store.setup().await?;
    let limit = TimeLimit::new("login:{email}", 2, 60, store)?;

    assert!(!limit.check_at(1_700_000_000).await?.limited);
    assert!(!limit.check_at(1_700_000_001).await?.limited);
    assert!(limit.check_at(1_700_000_002).await?.limited);

    // The next minute is a new window.
    assert!(!limit.check_at(1_700_000_040).await?.limited);
    Ok::<_, utopia_abuse::Error>(())
})?;

Build the key from the request

A key is a pattern with placeholders. with_params (or with_param for one) returns a copy whose key has them filled in, counting in the same store, so build the limit once and derive one per request. The longest placeholder that matches wins, and a value is never scanned for placeholders again.

Rust
use utopia_abuse::{Adapter, TimeLimit, time_limit};

let limit = TimeLimit::new("url:{url},ip:{ip}", 10, 60, time_limit::None)?;
let request = limit.with_params([("{url}", "/v1/account/sessions"), ("{ip}", "203.0.113.7")]);

assert_eq!(request.key(), "url:/v1/account/sessions,ip:203.0.113.7");
assert_eq!(limit.key(), "url:{url},ip:{ip}");

Answer with rate-limit headers

Every check answers a Verdict: whether the request is limited, the limit, the requests left after this one and when the window resets, ready for the X-RateLimit-* headers.

Rust
use utopia_abuse::{TimeLimit, time_limit};

tokio::runtime::Builder::new_current_thread().build()?.block_on(async {
    let limit = TimeLimit::new("ip:{ip}", 60, 3600, time_limit::None)?;
    let verdict = limit.check_at(1_700_000_000).await?;

    let headers = [
        ("X-RateLimit-Limit", verdict.limit.to_string()),
        ("X-RateLimit-Remaining", verdict.remaining.to_string()),
        ("X-RateLimit-Reset", verdict.reset.to_string()),
    ];
    assert_eq!(headers[2].1, "1700002800");
    Ok::<_, utopia_abuse::Error>(())
})?;

Count in Redis

The Redis stores keep one counter per key and window, abuse__<key>__<window start>, expiring with the window, and count with a script so concurrent servers never overshoot. PHP and Rust write the same keys and values, so both runtimes count against the same limits.

Rust
use utopia_abuse::{Adapter, TimeLimit, time_limit};

tokio::runtime::Builder::new_current_thread().enable_all().build()?.block_on(async {
    let conn = redis::Client::open("redis://localhost:6379")?.get_connection_manager().await?;
    let limit = TimeLimit::new("guide:{ip}", 2, 60, time_limit::Redis::new(conn))?.with_param("{ip}", "203.0.113.7");

    limit.reset().await?;
    assert!(!limit.check().await?.limited);
    assert!(!limit.check().await?.limited);
    assert!(limit.check().await?.limited);
    Ok::<_, Box<dyn std::error::Error>>(())
})?;
Compiles in CI; needs a running service to execute.

Smooth the window edges

A fixed window lets a client spend its whole limit at the end of one window and again at the start of the next. A SlidingWindow also counts the previous window, weighted by how much of it the last window seconds still cover.

Rust
use utopia_abuse::{SlidingWindow, sliding_window};

tokio::runtime::Builder::new_current_thread().build()?.block_on(async {
    let limit = SlidingWindow::new("ip:{ip}", 100, 60, sliding_window::None)?;
    let verdict = limit.check_at(1_700_000_000).await?;

    assert_eq!((verdict.limited, verdict.remaining, verdict.reset), (false, 99, 1_700_000_040));
    Ok::<_, utopia_abuse::Error>(())
})?;

Allow bursts with a token bucket

A TokenBucket holds up to tokens tokens and refills at refill_rate a second. Each request takes one, so a client can burst up to the capacity and then go on at the refill rate. The reset time is when the bucket will be full again.

Rust
use utopia_abuse::{TokenBucket, token_bucket};

tokio::runtime::Builder::new_current_thread().build()?.block_on(async {
    let limit = TokenBucket::new("ip:{ip}", 10, 0.5, token_bucket::None)?;
    let verdict = limit.check_at(1_700_000_000.0).await?;

    assert_eq!((verdict.limited, verdict.remaining, verdict.reset), (false, 9, 1_700_000_002));
    Ok::<_, utopia_abuse::Error>(())
})?;

Tell people from bots

ReCaptcha sends a reCAPTCHA v3 token to Google and passes it when Google answers with a score of at least the threshold. Give it your own Client to route or replay its traffic:

Rust
use utopia_abuse::ReCaptcha;
use utopia_abuse::recaptcha::{BoxFuture, Client, Request};

struct Google;

impl Client for Google {
    fn send<'a>(&'a self, _request: Request) -> BoxFuture<'a, utopia_abuse::Result<Vec<u8>>> {
        Box::pin(async { Ok(br#"{"success": true, "score": 0.7}"#.to_vec()) })
    }
}

tokio::runtime::Builder::new_current_thread().build()?.block_on(async {
    let recaptcha = ReCaptcha::with_client("site-secret", Google);
    assert!(recaptcha.verify("token", "203.0.113.7", 0.5).await?);
    assert!(!recaptcha.verify("token", "203.0.113.7", 0.9).await?);
    Ok::<_, utopia_abuse::Error>(())
})?;

Reference#

Rate limits that keep clients from abusing an API: how many requests a key (an IP, a user, an endpoint) may make, counted in fixed windows (TimeLimit), sliding windows (SlidingWindow) or token buckets (TokenBucket), stored in Redis, a Redis Cluster, a pool of connections, a document store or Appwrite tables. And ReCaptcha, to tell people from bots.

Every limit implements [Adapter]: Adapter::check records a request and says whether it is over the limit, Adapter::peek says the same without recording one, Adapter::reset forgets the current window. Each answers a Verdict with the limit, the requests left and when the window resets, ready for X-RateLimit-* headers. Each limit also has check_at, peek_at and reset_at, which take the time instead of reading the clock.

Rust
use utopia_abuse::{Adapter, TimeLimit, time_limit};

// Two requests per hour for each IP; None keeps no counts.
let limit = TimeLimit::new("ip:{ip}", 2, 3600, time_limit::None)?.with_param("{ip}", "203.0.113.7");
assert_eq!(limit.key(), "ip:203.0.113.7");
assert!(!limit.check().await?.limited);

Modules#

Each module groups one concept of the library. Internal ones hold items the crate re-exports at its root.

errorinternal
The error type shared by every rate limit and store.
keyinternal
`Key`: the name a limit counts under, a pattern with placeholders.
recaptcha5 items
`ReCaptcha`: asks Google whether a reCAPTCHA token came from a person.
redis_conninternal
Redis plumbing shared by the Redis stores: replies as phpredis reports them, key scans, and the `Pool` the `RedisPool` stores borrow from.
sliding_window4 items
Sliding windows: the current window's count plus the previous window's, weighted by how much of it the sliding window still covers.
sliding_window::redisinternal
Sliding-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.
time_limit::databaseinternal
Fixed-window counters as documents of an `abuse` collection: one per key and window, unique on both.
time_limit4 items
Fixed windows: at most `limit` requests per key in each window of `seconds`, the windows aligned on the Unix epoch.
time_limit::redisinternal
Fixed-window counters in Redis: one string per key and window, `abuse__<key>__<window start>`, expiring with the window.
time_limit::tablesinternal
Fixed-window counters as rows of an Appwrite table, through the TablesDB API.
token_bucket4 items
Token buckets: a key holds up to `tokens` tokens, refilled at `refill_rate` a second; each request takes one.
token_bucket::redisinternal
Token buckets in Redis: one hash per key, `abuse__<key>`, holding the balance (`tokens`) and when it was last refilled (`last_refill`).

API#

What utopia_abuse exports at its root.