an http based roguelike rpg about jelqing
Blame: src/http/registry.rs
159f0998263b | atagen | 2026-10-12 11:01 UTC | 1 | //! The live runs. The table is only locked long enough to find a run, and each run has its own |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 2 | //! lock, so one slow turn never holds up anyone else's. |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 3 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 4 | use std::{ |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 5 | collections::HashMap, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 6 | sync::{Arc, Mutex, MutexGuard, PoisonError, RwLock, TryLockError}, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 7 | time::{Duration, Instant}, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 8 | }; |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 9 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 10 | use crate::game::Run; |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 11 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 12 | pub type Slot = Arc<Mutex<Run>>; |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 13 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 14 | #[derive(Default)] |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 15 | pub struct Registry { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 16 | runs: RwLock<HashMap<String, Slot>>, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 17 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 18 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 19 | /// A panic mid-request must not take the run down with it, so a poisoned lock is recovered. |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 20 | pub fn hold(slot: &Slot) -> MutexGuard<'_, Run> { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 21 | slot.lock().unwrap_or_else(PoisonError::into_inner) |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 22 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 23 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 24 | impl Registry { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 25 | pub fn get(&self, id: &str) -> Option<Slot> { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 26 | self.read().get(id).cloned() |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 27 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 28 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 29 | pub fn len(&self) -> usize { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 30 | self.read().len() |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 31 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 32 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 33 | pub fn insert(&self, run: Run) { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 34 | let id = run.id.clone(); |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 35 | self.write().insert(id, Arc::new(Mutex::new(run))); |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 36 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 37 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 38 | pub fn remove(&self, id: &str) { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 39 | self.write().remove(id); |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 40 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 41 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 42 | pub fn contains(&self, id: &str) -> bool { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 43 | self.read().contains_key(id) |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 44 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 45 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 46 | /// Every live run, each visited under its own lock. |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 47 | pub fn each<T>(&self, mut visit: impl FnMut(&Run) -> T) -> Vec<T> { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 48 | let slots: Vec<Slot> = self.read().values().cloned().collect(); |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 49 | slots.iter().map(|slot| visit(&hold(slot))).collect() |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 50 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 51 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 52 | /// Takes out the runs nobody has touched for `ttl`, and hands them back for the record. |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 53 | pub fn evict_idle(&self, now: Instant, ttl: Duration) -> Vec<Slot> { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 54 | let mut runs = self.write(); |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 55 | let idle: Vec<String> = runs |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 56 | .iter() |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 57 | .filter(|(_, slot)| { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 58 | let idle = |run: &Run| now.duration_since(run.last_seen) >= ttl; |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 59 | match slot.try_lock() { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 60 | Ok(run) => idle(&run), |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 61 | Err(TryLockError::Poisoned(poisoned)) => idle(&poisoned.into_inner()), |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 62 | Err(TryLockError::WouldBlock) => false, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 63 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 64 | }) |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 65 | .map(|(id, _)| id.clone()) |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 66 | .collect(); |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 67 | idle.iter().filter_map(|id| runs.remove(id)).collect() |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 68 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 69 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 70 | fn read(&self) -> std::sync::RwLockReadGuard<'_, HashMap<String, Slot>> { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 71 | self.runs.read().unwrap_or_else(PoisonError::into_inner) |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 72 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 73 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 74 | fn write(&self) -> std::sync::RwLockWriteGuard<'_, HashMap<String, Slot>> { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 75 | self.runs.write().unwrap_or_else(PoisonError::into_inner) |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 76 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 77 | } |