https://github.com/harr1424/actix-route-rate-limiter
A rate_limit crate to rate limit endpoints on an Actix-Web server
https://github.com/harr1424/actix-route-rate-limiter
actix-web-middleware rate-limiter rust
Last synced: about 2 months ago
JSON representation
A rate_limit crate to rate limit endpoints on an Actix-Web server
- Host: GitHub
- URL: https://github.com/harr1424/actix-route-rate-limiter
- Owner: harr1424
- Created: 2024-07-19T15:14:02.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-21T22:38:57.000Z (11 months ago)
- Last Synced: 2025-05-04T02:39:29.613Z (6 months ago)
- Topics: actix-web-middleware, rate-limiter, rust
- Language: Rust
- Homepage: https://crates.io/crates/actix_route_rate_limiter
- Size: 47.9 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Actix Route Rate Limiter
A library crate that can be used to add rate limiting middleware to Actix Web Application routes.
```rust
use std::sync::Arc;
use actix_web::{web, App, HttpServer, HttpResponse};
use chrono::Duration;
use actix_route_rate_limiter::{LimiterBuilder, RateLimiter};#[actix_web::main]
pub async fn main() -> std::io::Result<()> {// build a limiter
let limiter = LimiterBuilder::new()
.with_duration(Duration::seconds(20)) // default value is one second
.with_num_requests(2) // default value is one request
.with_cleanup(Duration::minutes(1)) // default to no cleanup of stale entries
.build();HttpServer::new(move || {
App::new()
.wrap(RateLimiter::new(Arc::clone(&limiter)))
.route("/", web::get().to(HttpResponse::Ok))
})
.bind(("0.0.0.0", 12345))?
.run()
.await
}
```This crate can be used to wrap routes with rate limiting logic by defining a duration
and number of requests that will be forwarded during that duration.If a quantity of requests exceeds this amount, the middleware will short circuit the request and instead send an `HTTP 429 - Too Many Requests` response with headers describing the rate limit:
- `Retry-After` : the rate-limiting duration, begins at first request received and ends after this elapsed time
- `X-RateLimit-Limit` : number of requests allowed for the duration
- `X-RateLimit-Remaining` : number of requests remaining for current duration
- `X-RateLimit-Reset` : number of seconds remaining in the duration[Actix Web docs regarding App.wrap()](https://docs.rs/actix-web/latest/actix_web/struct.App.html#method.wrap)