Fix ratelimit build, tweak ratelimit example
This commit is contained in:
parent
679af3e313
commit
2ae721f131
|
@ -12,7 +12,7 @@ async fn main() -> Result<()> {
|
||||||
|
|
||||||
Server::new()
|
Server::new()
|
||||||
.add_route("/", handle_request)
|
.add_route("/", handle_request)
|
||||||
.ratelimit("/limit", 2, Duration::from_secs(60))
|
.ratelimit("/limit", 2, Duration::from_secs(10))
|
||||||
.serve_ip("localhost:1965")
|
.serve_ip("localhost:1965")
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ async fn handle_request(request: Request) -> Result<Response> {
|
||||||
|
|
||||||
if let Some("limit") = request.trailing_segments().get(0).map(String::as_str) {
|
if let Some("limit") = request.trailing_segments().get(0).map(String::as_str) {
|
||||||
document.add_text("You're on a rate limited page!")
|
document.add_text("You're on a rate limited page!")
|
||||||
.add_text("You can only access this page twice per minute");
|
.add_text("You can only access this page twice every 10 seconds");
|
||||||
} else {
|
} else {
|
||||||
document.add_text("You're on a normal page!")
|
document.add_text("You're on a normal page!")
|
||||||
.add_text("You can access this page as much as you like.");
|
.add_text("You can access this page as much as you like.");
|
||||||
|
|
|
@ -1056,12 +1056,11 @@ async fn send_response_body(mut body: Option<Body>, stream: &mut (impl AsyncWrit
|
||||||
|
|
||||||
#[cfg(feature="ratelimiting")]
|
#[cfg(feature="ratelimiting")]
|
||||||
/// Every 5 minutes, remove excess keys from all ratelimiters
|
/// Every 5 minutes, remove excess keys from all ratelimiters
|
||||||
async fn prune_ratelimit_log(rate_limits: Arc<RoutingNode<RateLimiter<IpAddr>>>) -> Never {
|
async fn prune_ratelimit_log(rate_limits: &'static RoutingNode<RateLimiter<IpAddr>>) -> Never {
|
||||||
let mut interval = interval(tokio::time::Duration::from_secs(10));
|
let mut interval = interval(tokio::time::Duration::from_secs(10));
|
||||||
let log = rate_limits.as_ref();
|
|
||||||
loop {
|
loop {
|
||||||
interval.tick().await;
|
interval.tick().await;
|
||||||
log.iter().for_each(RateLimiter::trim_keys_verbose);
|
rate_limits.iter().for_each(RateLimiter::trim_keys_verbose);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue