Fix ratelimit build, tweak ratelimit example

This commit is contained in:
Emi Tatsuo 2020-12-16 10:17:27 -05:00
parent 679af3e313
commit 2ae721f131
Signed by: Emi
GPG key ID: 68FAB2E2E6DFC98B
2 changed files with 4 additions and 5 deletions

View file

@ -12,7 +12,7 @@ async fn main() -> Result<()> {
Server::new()
.add_route("/", handle_request)
.ratelimit("/limit", 2, Duration::from_secs(60))
.ratelimit("/limit", 2, Duration::from_secs(10))
.serve_ip("localhost:1965")
.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) {
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 {
document.add_text("You're on a normal page!")
.add_text("You can access this page as much as you like.");

View file

@ -1056,12 +1056,11 @@ async fn send_response_body(mut body: Option<Body>, stream: &mut (impl AsyncWrit
#[cfg(feature="ratelimiting")]
/// 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 log = rate_limits.as_ref();
loop {
interval.tick().await;
log.iter().for_each(RateLimiter::trim_keys_verbose);
rate_limits.iter().for_each(RateLimiter::trim_keys_verbose);
}
}