Rust

sliding_window

Module

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

A store that keeps nothing: every request passes.

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

The settings of the limit a Store counts for.

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

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

Where a SlidingWindow keeps its counts: one bucket per key and window.

sliding_window::redis

Own page

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.

use utopia_abuse::sliding_window::redis::Redis;
Rust
pub struct Redis<C = ConnectionManager>

Buckets in one Redis server, each living ttl seconds.

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

Buckets in a Redis Cluster, each living ttl seconds.

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

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

Also implements Clone, Debug.