expression

REST: fixed-window request quota

Count accepted requests in a fixed time bucket. This model permits two requests per ten seconds and resets at the next boundary. It uses synthetic seconds rather than wall-clock time. A fixed window can permit a burst across adjacent boundaries. Standalone synthetic example. Expected stdout (not executed): 0: true 1: true 2: false 10: true 11: true Common mistake: Treating a local counter as an atomic distributed quota.

Formal Syntax & Structure

rust syntaxrust
window = now / window_size

Working Example

Example Snippetrust
use axum::{Router, Json, body::{Body, to_bytes}, http::{Request, StatusCode}};use serde::{Deserialize, Serialize};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() {    let mut window = 0;    let mut used = 0;    for now in [0, 1, 2, 10, 11] {        if now / 10 != window { window = now / 10; used = 0; }        let allowed = used < 2;        if allowed { used += 1; }        println!("{now}: {allowed}");    }}
Rust Reference