rainier_middleware/
rate_limit.rs1use std::collections::HashMap;
25use std::sync::Mutex;
26use std::time::{Duration, Instant};
27
28use rainier_support::{BoxFuture, Result};
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub struct Hit {
33 pub count: u32,
36 pub resets_in: Duration,
38}
39
40pub trait RateLimitStore: Send + Sync + 'static {
49 fn hit<'a>(&'a self, key: &'a str, window: Duration) -> BoxFuture<'a, Result<Hit>>;
54
55 fn peek<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<Option<Hit>>>;
61
62 fn clear<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<()>>;
67
68 fn is_shared(&self) -> bool;
74
75 fn name(&self) -> &str;
77}
78
79#[derive(Debug, Default)]
85pub struct MemoryRateLimitStore {
86 windows: Mutex<HashMap<String, Window>>,
87}
88
89#[derive(Debug, Clone, Copy)]
90struct Window {
91 count: u32,
92 started: Instant,
93 length: Duration,
94}
95
96impl Window {
97 fn is_live(&self, now: Instant) -> bool {
98 now.duration_since(self.started) < self.length
99 }
100
101 fn resets_in(&self, now: Instant) -> Duration {
102 self.length.saturating_sub(now.duration_since(self.started))
103 }
104}
105
106impl MemoryRateLimitStore {
107 pub fn new() -> Self {
109 Self::default()
110 }
111
112 pub fn tracked(&self) -> usize {
117 self.windows.lock().expect("rate limit lock poisoned").len()
118 }
119}
120
121impl RateLimitStore for MemoryRateLimitStore {
122 fn hit<'a>(&'a self, key: &'a str, window: Duration) -> BoxFuture<'a, Result<Hit>> {
123 Box::pin(async move {
124 let now = Instant::now();
125 let mut windows = self.windows.lock().expect("rate limit lock poisoned");
126
127 windows.retain(|_, existing| existing.is_live(now));
132
133 let existing = windows.entry(key.to_string()).or_insert(Window {
134 count: 0,
135 started: now,
136 length: window,
137 });
138
139 existing.count += 1;
140 Ok(Hit { count: existing.count, resets_in: existing.resets_in(now) })
141 })
142 }
143
144 fn peek<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<Option<Hit>>> {
145 Box::pin(async move {
146 let now = Instant::now();
147 let windows = self.windows.lock().expect("rate limit lock poisoned");
148
149 Ok(windows
150 .get(key)
151 .filter(|window| window.is_live(now))
152 .map(|window| Hit { count: window.count, resets_in: window.resets_in(now) }))
153 })
154 }
155
156 fn clear<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<()>> {
157 Box::pin(async move {
158 self.windows.lock().expect("rate limit lock poisoned").remove(key);
159 Ok(())
160 })
161 }
162
163 fn is_shared(&self) -> bool {
164 false
165 }
166
167 fn name(&self) -> &str {
168 "memory"
169 }
170}
171
172#[cfg(test)]
173mod tests {
174 use super::*;
175
176 fn store() -> MemoryRateLimitStore {
177 MemoryRateLimitStore::new()
178 }
179
180 #[tokio::test]
181 async fn hits_count_up_within_a_window() {
182 let store = store();
183 let window = Duration::from_secs(60);
184
185 for expected in 1..=3 {
186 let hit = store.hit("ada", window).await.unwrap();
187 assert_eq!(hit.count, expected);
188 }
189 }
190
191 #[tokio::test]
192 async fn keys_are_counted_separately() {
193 let store = store();
194 let window = Duration::from_secs(60);
195
196 store.hit("ada", window).await.unwrap();
197 store.hit("ada", window).await.unwrap();
198 let grace = store.hit("grace", window).await.unwrap();
199
200 assert_eq!(grace.count, 1);
201 assert_eq!(store.peek("ada").await.unwrap().unwrap().count, 2);
202 }
203
204 #[tokio::test]
205 async fn a_window_expires_and_the_allowance_comes_back() {
206 let store = store();
207 let window = Duration::from_millis(30);
208
209 store.hit("ada", window).await.unwrap();
210 store.hit("ada", window).await.unwrap();
211
212 tokio::time::sleep(Duration::from_millis(50)).await;
213
214 assert_eq!(store.hit("ada", window).await.unwrap().count, 1);
215 }
216
217 #[tokio::test]
218 async fn an_expired_window_reads_as_nothing_rather_than_as_a_stale_count() {
219 let store = store();
220 let window = Duration::from_millis(30);
221
222 store.hit("ada", window).await.unwrap();
223 tokio::time::sleep(Duration::from_millis(50)).await;
224
225 assert!(store.peek("ada").await.unwrap().is_none());
226 }
227
228 #[tokio::test]
229 async fn dead_keys_are_evicted_rather_than_accumulating() {
230 let store = store();
234 let window = Duration::from_millis(30);
235
236 for i in 0..100 {
237 store.hit(&format!("ip:{i}"), window).await.unwrap();
238 }
239 assert_eq!(store.tracked(), 100);
240
241 tokio::time::sleep(Duration::from_millis(50)).await;
242 store.hit("ip:fresh", window).await.unwrap();
243
244 assert_eq!(store.tracked(), 1, "the expired keys were not evicted");
245 }
246
247 #[tokio::test]
248 async fn clearing_restores_the_whole_allowance() {
249 let store = store();
250 let window = Duration::from_secs(60);
251
252 store.hit("ada", window).await.unwrap();
253 store.hit("ada", window).await.unwrap();
254 store.clear("ada").await.unwrap();
255
256 assert!(store.peek("ada").await.unwrap().is_none());
257 assert_eq!(store.hit("ada", window).await.unwrap().count, 1);
258 }
259
260 #[tokio::test]
261 async fn the_reset_time_counts_down() {
262 let store = store();
263 let window = Duration::from_secs(60);
264
265 let first = store.hit("ada", window).await.unwrap();
266 tokio::time::sleep(Duration::from_millis(20)).await;
267 let second = store.hit("ada", window).await.unwrap();
268
269 assert!(
270 second.resets_in < first.resets_in,
271 "{:?} !< {:?}",
272 second.resets_in,
273 first.resets_in
274 );
275 assert_eq!(second.count, 2);
277 }
278
279 #[test]
280 fn it_says_it_is_not_shared() {
281 assert!(!store().is_shared());
282 assert_eq!(store().name(), "memory");
283 }
284}