//! Bounds on the outbound work signed-in users can start: feed downloads //! they ask for ("add RSS", manual refresh) and audio proxy streams. Each //! kind has a per-account and a server-wide limit on requests in flight, so //! one account cannot tie up the server's memory, connections or bandwidth. //! Scheduled refreshes are bounded by the job itself (`runner::RUN_LIMIT`, //! `runner::RUN_CONCURRENCY`, `runner::RUN_PER_ACCOUNT`). use std::collections::HashMap; use std::sync::{Arc, Mutex}; /// Feed downloads one account may run at once ("add RSS", refresh). pub(crate) const FEED_FETCHES_PER_ACCOUNT: usize = 2; /// Feed downloads started by users, server-wide (each may buffer up to /// `fetch::MAX_FEED_BYTES`). pub(crate) const FEED_FETCHES_TOTAL: usize = 16; /// Audio proxy responses one account may stream at once (a player opens a /// new range request when seeking while the old one is still closing). pub(crate) const PROXY_STREAMS_PER_ACCOUNT: usize = 4; /// Audio proxy responses streamed at once, server-wide. pub(crate) const PROXY_STREAMS_TOTAL: usize = 64; /// Seconds a refused client is asked to wait (`Retry-After`). pub(crate) const RETRY_AFTER_SECS: u64 = 5; #[derive(Debug, Default)] struct Counts { total: usize, accounts: HashMap, } /// Requests in flight per account and in total. #[derive(Debug, Clone)] pub(crate) struct ConcurrencyLimit { per_account: usize, total: usize, counts: Arc>, } impl ConcurrencyLimit { pub(crate) fn new(per_account: usize, total: usize) -> Self { Self { per_account, total, counts: Arc::default(), } } /// A slot for one request of `user_id`, released when the permit is /// dropped; `None` when the account or the server is at its limit. pub(crate) fn try_acquire(&self, user_id: i64) -> Option { let mut counts = self .counts .lock() .unwrap_or_else(|error| error.into_inner()); let account = counts.accounts.get(&user_id).copied().unwrap_or(0); if account >= self.per_account || counts.total >= self.total { return None; } counts.total += 1; counts.accounts.insert(user_id, account + 1); Some(Permit { user_id, counts: Arc::clone(&self.counts), }) } #[cfg(test)] fn in_flight(&self, user_id: i64) -> (usize, usize) { let counts = self.counts.lock().unwrap(); ( counts.accounts.get(&user_id).copied().unwrap_or(0), counts.total, ) } } /// One request in flight (see [`ConcurrencyLimit::try_acquire`]). #[derive(Debug)] pub(crate) struct Permit { user_id: i64, counts: Arc>, } impl Drop for Permit { fn drop(&mut self) { let mut counts = self .counts .lock() .unwrap_or_else(|error| error.into_inner()); counts.total = counts.total.saturating_sub(1); if let Some(account) = counts.accounts.get_mut(&self.user_id) { *account = account.saturating_sub(1); if *account == 0 { counts.accounts.remove(&self.user_id); } } } } #[cfg(test)] mod tests { use super::*; #[test] fn accounts_and_the_server_are_bounded_until_permits_are_dropped() { let limit = ConcurrencyLimit::new(2, 3); let a1 = limit.try_acquire(1).unwrap(); let a2 = limit.try_acquire(1).unwrap(); assert!(limit.try_acquire(1).is_none(), "per account"); let b1 = limit.try_acquire(2).unwrap(); assert!(limit.try_acquire(3).is_none(), "server-wide"); assert_eq!(limit.in_flight(1), (2, 3)); drop(a1); assert_eq!(limit.in_flight(1), (1, 2)); let a3 = limit.try_acquire(1).unwrap(); drop((a2, a3, b1)); assert_eq!(limit.in_flight(1), (0, 0)); assert!(limit.counts.lock().unwrap().accounts.is_empty()); // Clones share the counts. let clone = limit.clone(); let _held = clone.try_acquire(4).unwrap(); assert_eq!(limit.in_flight(4), (1, 1)); } }