http-rate

Crates.iohttp-rate
lib.rshttp-rate
version0.1.1
sourcesrc
created_at2024-01-19 04:08:18.501576
updated_at2024-01-19 21:29:28.631168
descriptionrate limit for http crate types
homepage
repositoryhttps://github.com/HFQR/xitca-web
max_upload_size
id1104957
size68,798
(fakeshadow)

documentation

README

rate limit for http types

use std::net::SocketAddr;

use http::{Request, Response};
use http_rate::{Quota, RateLimit};

// a quota limiting request to 1 per second.
let quota = Quota::per_second(1);

// construct rate limiter with given quota.
let limiter = RateLimit::new(quota);

async fn request(lim: &RateLimit, req: &Request<()>, addr: SocketAddr) -> Response<()> {
    // rate limiter needs request header map and client socket addr.
    match lim.rate_limit(req.headers(), &addr) {
        // client still have quota left.
        Ok(snap) => {
            // rate limiter guarded logic can be executed here. 
            // in this case a dummy response is constructed.
            let mut res = Response::new(());

            // snapshot return from rate_limit method can be used to extend
            // rate-limit related headers to response.
            snap.extend_response(&mut res);

            res
        }
        // client ran out of quota.
        Err(e) => {
            // logic not guarded by rate limiter can be executed here.
            // in this case a dummy response is constructed.
            let mut res = Response::new(());

            // error return from rate_limit method can be used to extend rate-limit
            // related headers and status code to error response.
            e.extend_response(&mut res);

            // error can be formatted.
            println!("{e:?}");
            println!("{e}");

            // error can be boxed as std::error::Error trait object.
            let _ = Box::new(e) as Box<dyn std::error::Error + Send + Sync>;

            res
        }
    }
}
Commit count: 1381

cargo fmt