oxedyne/fe2o3/fe2o3_net/src/guard/floor.rs
2.7 KiB, 1 run
created by r1870400018:65636, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! A minimum interval between two accepted calls under one string key. |
| 2 | //! |
| 3 | //! Lifted from `/api/debug-trace` in Daimond's gateway, where it kept one account and device |
| 4 | //! from posting a diagnostic trace more than once every two seconds. `addr::AddressGuard` rates |
| 5 | //! an address and escalates to a blacklist, `user::UserGuard` holds trust state and |
| 6 | //! `nonce::NonceTracker` refuses a replay, so none of them answers "has this key spoken within |
| 7 | //! the last N milliseconds", which is a floor and not a window. |
| 8 | |
| 9 | use oxedyne_fe2o3_core::prelude::*; |
| 10 | |
| 11 | use std::{ |
| 12 | collections::HashMap, |
| 13 | sync::Mutex, |
| 14 | }; |
| 15 | |
| 16 | |
| 17 | /// Remembers when each key was last admitted, and refuses a key that returns too soon. |
| 18 | /// |
| 19 | /// The caller supplies the clock, so a test can say "1.5 seconds later" without waiting. A |
| 20 | /// refused call leaves the record alone: a client hammering the floor does not hold it open. |
| 21 | /// Entries are let go once they are older than the time to live, but only when the map has grown |
| 22 | /// past a bound, so a quiet guard never walks its own map. |
| 23 | #[derive(Debug)] |
| 24 | pub struct KeyFloor { |
| 25 | min_ms: u64, |
| 26 | ttl_ms: u64, |
| 27 | prune_at: usize, |
| 28 | last: Mutex<HashMap<String, u64>>, // key -> millisecond of the last admitted call |
| 29 | } |
| 30 | |
| 31 | impl KeyFloor { |
| 32 | |
| 33 | pub const DEFAULT_TTL_MS: u64 = 3_600_000; |
| 34 | pub const DEFAULT_PRUNE_AT: usize = 4_096; |
| 35 | |
| 36 | pub fn new(min_ms: u64) -> Self { |
| 37 | Self { |
| 38 | min_ms, |
| 39 | ttl_ms: Self::DEFAULT_TTL_MS, |
| 40 | prune_at: Self::DEFAULT_PRUNE_AT, |
| 41 | last: Mutex::new(HashMap::new()), |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// How long an entry is kept before it may be pruned. It must outlast the floor, or a live |
| 46 | /// key would be forgotten mid-interval. |
| 47 | pub fn with_ttl_ms(mut self, ttl_ms: u64) -> Self { |
| 48 | self.ttl_ms = ttl_ms.max(self.min_ms); |
| 49 | self |
| 50 | } |
| 51 | |
| 52 | /// The size the map may reach before the stale entries are swept out. |
| 53 | pub fn with_prune_at(mut self, prune_at: usize) -> Self { |
| 54 | self.prune_at = prune_at; |
| 55 | self |
| 56 | } |
| 57 | |
| 58 | /// Is the key far enough past its last admitted call? Records the call when it is. |
| 59 | pub fn admit(&self, key: &str, now_ms: u64) -> Outcome<bool> { |
| 60 | let mut map = lock_mutex!(self.last); |
| 61 | if map.len() > self.prune_at { |
| 62 | let ttl = self.ttl_ms; |
| 63 | map.retain(|_, &mut last| now_ms.saturating_sub(last) < ttl); |
| 64 | } |
| 65 | if let Some(&last) = map.get(key) { |
| 66 | if now_ms.saturating_sub(last) < self.min_ms { |
| 67 | return Ok(false); |
| 68 | } |
| 69 | } |
| 70 | map.insert(key.to_string(), now_ms); |
| 71 | Ok(true) |
| 72 | } |
| 73 | |
| 74 | /// How many keys are held. |
| 75 | pub fn len(&self) -> Outcome<usize> { |
| 76 | let map = lock_mutex!(self.last); |
| 77 | Ok(map.len()) |
| 78 | } |
| 79 | } |