Crates.io | warp-rate-limit |
lib.rs | warp-rate-limit |
version | 0.1.0 |
source | src |
created_at | 2024-11-23 00:56:30.220217 |
updated_at | 2024-11-23 00:56:30.220217 |
description | Rate limiting middleware for the Warp web framework |
homepage | |
repository | https://github.com/jesselawson/warp-rate-limit |
max_upload_size | |
id | 1458088 |
size | 51,567 |
A rate-limiting middleware for the Warp web framework.
Features:
Add this to your Cargo.toml
:
[dependencies]
warp-rate-limit = "0.1"
Then, you'll need to:
Here's an example:
use std::time::Duration;
use warp::{Filter, Reply};
use warp_rate_limit::RateLimit;
#[tokio::main]
async fn main() {
// STEP 1. Create a rate limiter that allows, at most,
// 5 requests per every 30 seconds:
let rate_limit = RateLimit::new()
.with_window(Duration::from_secs(30))
.with_max_requests(5);
// STEP 2. Add the rate limiter to a route:
let route = warp::path("hello")
.and(rate_limit)
.map(|_| "Hello, World!");
warp::serve(route).run(([127, 0, 0, 1], 3030)).await;
}
See the documentation.
For most basic web applications these limitations are acceptable, but if you need more advanced features, consider using a dedicated rate-limiting solution with persistent storage.
HashMap
. This means:
If any of these limitations are a blocker for you, consider augmenting the crate and submitting a PR.
Contributions are welcome and appreciated. Submit a PR when you're ready.