Governor Rate Limit Middleware

Apply the governor quota-based limiter with per-key direct limiters, burst capacity, and 429 responses carrying retry timing.

RustResilience

Code

rustsnippet
68 lines
use governor::{
    clock::DefaultClock, middleware::NoOpMiddleware, state::direct::NotKeyed,
    state::InMemoryState, Quota, RateLimiter,
};
use std::num::NonZeroU32;
use std::sync::Arc;
use std::time::Duration;

type SharedLimiter = RateLimiter<
    NotKeyed,
    InMemoryState,
    DefaultClock,
    NoOpMiddleware,
>;

#[derive(Clone)]
struct ApiGate {
    limiter: Arc<SharedLimiter>,
}

impl ApiGate {
    fn new(per_minute: u32) -> Self {
        let quota = Quota::per_minute(NonZeroU32::new(per_minute).expect("positive"));
        Self {
            limiter: Arc::new(RateLimiter::direct(quota)),
        }
    }

    fn check(&self) -> Result<(), Duration> {
        match self.limiter.check() {
            Ok(_) => Ok(()),
            Err(negative) => Err(negative.wait_time_from_now()),
        }
    }
}

async fn handle_request(
    gate: &ApiGate,
    route: &str,
) -> Result<String, (u16, String)> {
    if let Err(wait) = gate.check() {
        let retry_after = wait.as_secs().max(1);
        return Err((
            429,
            format!("rate limited, retry after {retry_after}s"),
        ));
    }
    Ok(format!("{route} served"))
}

#[tokio::main]
async fn main() {
    let gate = ApiGate::new(5);

    let mut results = Vec::new();
    for i in 0..8 {
        let outcome = handle_request(&gate, "/search").await;
        let rendered = match outcome {
            Ok(body) => body,
            Err((status, message)) => format!("HTTP {status}: {message}"),
        };
        results.push(format!("req {i}: {rendered}"));
    }

    for line in results {
        println!("{line}");
    }
}

How it works

Count the functions new, check, handle_request and main as the entire surface area of Governor Rate Limit Middleware.

Repetition is confined to for i in 0..8 — the rest of the file is straight-line. Fallible operations return Result, forcing the caller to handle both arms explicitly. A match enumerates the value’s possible shapes, and the compiler rejects any arm you forget.

Caveats

  • Plan for an install step: governor:: is a real dependency, resolved by the package manager.
  • Errors travel through Result; if you drop the ? operators while adapting, each call site needs its own match.

Applying it

The main signature reads fn new(per_minute: u32) -> Self

Treat main as the orchestrator calling new and handle_request in a fixed order.

Statements that matter

return Err((
429,
format!("rate limited, retry after {retry_after}s"),
));
fn check(&self) -> Result<(), Duration> {
match self.limiter.check() {
Ok(_) => Ok(()),
Err(negative) => Err(negative.wait_time_from_now()),
}
}
fn new(per_minute: u32) -> Self {
let quota = Quota::per_minute(NonZeroU32::new(per_minute).expect("positive"));
Self {
limiter: Arc::new(RateLimiter::direct(quota)),
}
}

Interactive Tools for This Topic

FAQ

How are errors reported?
Through the type system: failures are `Result` variants the caller must consume.
Do I need to install anything before running this?
Yes: `governor::` must be present in the project or the import fails at load time.