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
43
44
45
46
47
48
//! Backend-independent rate-limit storage contract.
use Duration;
use async_trait;
use ;
/// Atomic token-bucket decisions using a clock owned by the store.
///
/// Local stores use a monotonic clock. Shared stores must use one authoritative
/// backend clock for checks and cleanup, never persist a process-local
/// `std::time::Instant` or translate it into a wall-clock timestamp. For a
/// PostgreSQL adapter, sample `clock_timestamp()` after locking the relevant
/// row; `CURRENT_TIMESTAMP` is the transaction start time and can be stale after
/// waiting for a lock. Clamp a backwards clock to the bucket's last observed
/// time so that a later recovery cannot refill the same interval twice.
///
/// A shared backend needs a stable namespace and atomic per-key serialization
/// across all instances. Persist its policy or policy fingerprint and reject
/// incompatible configurations without changing balances. Refilling, consuming
/// a token, and recording activity form one atomic operation; return success
/// only after it commits. Unknown commit outcomes must fail closed rather than
/// silently fall back to a fresh local bucket. Retained balances survive client
/// restarts. Pool size, query deadlines and storage quotas belong to the backend.