1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! Rate-limit keys: how a request is identified for counting.
use Future;
use crateResult;
use crateRequestContext;
/// Produces the key a request is rate-limited by (the "tracker").
///
/// The default is the client IP ([`ByIp`]). Implement this on a unit type to key
/// by something else — a user id, an API key, a tenant — reusing dependency
/// injection inside, since it has the full [`RequestContext`]:
///
/// ```ignore
/// struct ByUser;
/// impl ThrottleKey for ByUser {
/// async fn throttle_key(ctx: &RequestContext) -> tork::Result<String> {
/// Ok(CurrentUser::from_request(ctx).await?.id.to_string())
/// }
/// }
/// ```
/// The default key strategy: the client IP address.
///
/// Falls back to `"unknown"` when no peer address is available (for example an
/// in-process test request), so such requests share one counter rather than going
/// unlimited.
;