Expand description
Abuse protection beyond the per-minute rate limit: per-client bursts, per-route quotas, and global load shedding that spares trusted clients.
An AbusePolicy, set with
ServerBuilder::abuse_policy,
is checked for every request whose client address is known, before
the WAF and routing. The checks run in this order and the first
limit exceeded answers:
- Burst: at most
Limit::max_requestsper client inLimit::window_ms.429 Too Many Requests. - Route quota: per client, for requests that match the named
Route.429 Too Many Requests. - Global: all clients together. Untrusted clients get
503 Service Unavailableonce it is exceeded; clients inAbusePolicy::trustedare still served.
Routes listed in AbusePolicy::exempt_routes skip every check.
Counting uses fixed windows. Each refusal logs a warn line naming
the reason, the scope and the client, carries a Retry-After
header, and is counted in stats and on /metrics.
§Examples
use http_handle::abuse::{AbusePolicy, Limit};
use http_handle::Server;
let policy = AbusePolicy {
burst: Some(Limit { max_requests: 20, window_ms: 1_000 }),
trusted: vec!["10.0.0.0/8".into()],
..AbusePolicy::default()
};
let server = Server::builder()
.address("127.0.0.1:0")
.document_root(".")
.abuse_policy(policy)
.expect("valid policy")
.build()
.expect("server");
assert!(server.abuse_policy().is_some());Structs§
- Abuse
Event - A refused request, as logged.
- Abuse
Policy - Abuse controls. Every part is optional.
- Abuse
Stats - Counts of refused requests since the process started.
- Limit
- A request budget over a fixed window.
- Route
Quota - A per-client quota for one route.
Enums§
- Abuse
Error - Why an abuse policy was rejected.
- Abuse
Reason - Which limit refused a request.
Functions§
- stats
- Returns the abuse counters.