|
| 1 | +use aionui_common::{TimestampMs, now_ms}; |
| 2 | +use dashmap::DashMap; |
| 3 | + |
| 4 | +pub const ACTIVE_LEASE_TTL_MS: TimestampMs = 90_000; |
| 5 | + |
| 6 | +#[derive(Debug)] |
| 7 | +pub struct ActiveLeaseRegistry { |
| 8 | + leases: DashMap<String, TimestampMs>, |
| 9 | + ttl_ms: TimestampMs, |
| 10 | +} |
| 11 | + |
| 12 | +impl ActiveLeaseRegistry { |
| 13 | + pub fn new() -> Self { |
| 14 | + Self::with_ttl_ms(ACTIVE_LEASE_TTL_MS) |
| 15 | + } |
| 16 | + |
| 17 | + pub fn with_ttl_ms(ttl_ms: TimestampMs) -> Self { |
| 18 | + Self { |
| 19 | + leases: DashMap::new(), |
| 20 | + ttl_ms, |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + pub fn renew(&self, conversation_id: &str) -> TimestampMs { |
| 25 | + let expires_at = now_ms().saturating_add(self.ttl_ms); |
| 26 | + self.leases.insert(conversation_id.to_owned(), expires_at); |
| 27 | + expires_at |
| 28 | + } |
| 29 | + |
| 30 | + pub fn renew_many<'a>(&self, conversation_ids: impl IntoIterator<Item = &'a str>) -> (usize, TimestampMs) { |
| 31 | + let expires_at = now_ms().saturating_add(self.ttl_ms); |
| 32 | + let mut count = 0; |
| 33 | + for conversation_id in conversation_ids { |
| 34 | + self.leases.insert(conversation_id.to_owned(), expires_at); |
| 35 | + count += 1; |
| 36 | + } |
| 37 | + (count, expires_at) |
| 38 | + } |
| 39 | + |
| 40 | + pub fn active_until(&self, conversation_id: &str) -> Option<TimestampMs> { |
| 41 | + let expires_at = *self.leases.get(conversation_id)?; |
| 42 | + if expires_at > now_ms() { |
| 43 | + Some(expires_at) |
| 44 | + } else { |
| 45 | + self.leases.remove(conversation_id); |
| 46 | + None |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + pub fn is_active(&self, conversation_id: &str) -> bool { |
| 51 | + self.active_until(conversation_id).is_some() |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl Default for ActiveLeaseRegistry { |
| 56 | + fn default() -> Self { |
| 57 | + Self::new() |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(test)] |
| 62 | +mod tests { |
| 63 | + use super::*; |
| 64 | + use std::time::Duration; |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn renew_sets_active_lease() { |
| 68 | + let registry = ActiveLeaseRegistry::new(); |
| 69 | + |
| 70 | + let expires_at = registry.renew("conv-1"); |
| 71 | + |
| 72 | + assert_eq!(registry.active_until("conv-1"), Some(expires_at)); |
| 73 | + assert!(registry.is_active("conv-1")); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn renew_many_sets_all_leases_and_returns_count() { |
| 78 | + let registry = ActiveLeaseRegistry::new(); |
| 79 | + |
| 80 | + let (count, expires_at) = registry.renew_many(["conv-1", "conv-2"]); |
| 81 | + |
| 82 | + assert_eq!(count, 2); |
| 83 | + assert_eq!(registry.active_until("conv-1"), Some(expires_at)); |
| 84 | + assert_eq!(registry.active_until("conv-2"), Some(expires_at)); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn active_lookup_returns_none_for_missing_lease() { |
| 89 | + let registry = ActiveLeaseRegistry::new(); |
| 90 | + |
| 91 | + assert_eq!(registry.active_until("missing"), None); |
| 92 | + assert!(!registry.is_active("missing")); |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn active_lookup_lazily_removes_expired_lease() { |
| 97 | + let registry = ActiveLeaseRegistry::with_ttl_ms(1); |
| 98 | + registry.renew("conv-1"); |
| 99 | + |
| 100 | + std::thread::sleep(Duration::from_millis(5)); |
| 101 | + |
| 102 | + assert_eq!(registry.active_until("conv-1"), None); |
| 103 | + assert!(!registry.leases.contains_key("conv-1")); |
| 104 | + } |
| 105 | +} |
0 commit comments