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
49
50
51
52
53
54
55
56
//! [`RateLimitCounter`] — read-modify-write counters for rate limiting.
use Result;
use async_trait;
/// Atomic counters keyed by an arbitrary string, used for rate limiting.
///
/// The defining operation is [`increment`](RateLimitCounter::increment): it must
/// apply the read-modify-write **atomically** so two concurrent callers can never
/// observe or commit the same pre-increment value. Counters are scoped to a
/// fixed-length window; `window_secs` lets the backend bucket and expire counts
/// without the caller tracking wall-clock time.
///
/// # Example
///
/// ```
/// use aa_core::storage::{RateLimitCounter, Result};
/// use async_trait::async_trait;
///
/// /// A counter that always reports a single hit (a stand-in for a real backend).
/// struct AlwaysOne;
///
/// #[async_trait]
/// impl RateLimitCounter for AlwaysOne {
/// async fn increment(&self, _key: &str, _amount: u64, _window_secs: u64) -> Result<u64> {
/// Ok(1)
/// }
///
/// async fn current(&self, _key: &str) -> Result<u64> {
/// Ok(1)
/// }
///
/// async fn reset(&self, _key: &str) -> Result<()> {
/// Ok(())
/// }
/// }
/// ```