Skip to main content

Module abuse

Module abuse 

Source
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:

  1. Burst: at most Limit::max_requests per client in Limit::window_ms. 429 Too Many Requests.
  2. Route quota: per client, for requests that match the named Route. 429 Too Many Requests.
  3. Global: all clients together. Untrusted clients get 503 Service Unavailable once it is exceeded; clients in AbusePolicy::trusted are 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§

AbuseEvent
A refused request, as logged.
AbusePolicy
Abuse controls. Every part is optional.
AbuseStats
Counts of refused requests since the process started.
Limit
A request budget over a fixed window.
RouteQuota
A per-client quota for one route.

Enums§

AbuseError
Why an abuse policy was rejected.
AbuseReason
Which limit refused a request.

Functions§

stats
Returns the abuse counters.