//! The state shared by every route. Topcoat hands it to each handler through the app context. use std::collections::HashMap; use std::sync::{Arc, Mutex, PoisonError}; use in_core::blob::BlobStore; use in_core::config::Config; use in_core::store::Store; use crate::backup::BackupTrigger; use crate::family::FamilyCache; use crate::live::LiveHub; use crate::oidc::OidcClient; use crate::throttle::ShareThrottles; /// Everything a request handler reaches through the app context. Every field is cheap to clone. #[allow(dead_code)] // The live hub is read by the live pages, which come after sign-in. #[derive(Clone)] pub struct AppState { /// The configuration the process booted with. pub config: Arc, /// The database. Reads go to a pooled reader and writes to the single writer. pub store: Store, /// The blob store the configuration names: the local blob tree or an S3-compatible bucket. pub blobs: Arc, /// The instance key (`in.key`). Never used directly: every seal and MAC uses a subkey derived from it (see /// `in_core::secret`). pub key: Arc<[u8; 32]>, /// The change counters of the live pages. pub live: Arc, /// Asks the backup worker for a run now (the admin page). Shared with the worker. pub backup: Arc, /// The OpenID Connect client of `im`. `None` when the configuration has no `[oidc]` table, in which /// case nobody can sign in and the sign-in routes say so. pub oidc: Option>, /// The failure windows of share-link passwords. pub throttles: Arc, /// The switcher list of the family, refreshed by the family worker. pub family: Arc, /// The bytes object writes may stage right now, per service. pub staging: Arc, } /// Bytes of object writes that are being staged, per service. A write reserves the bytes it may stage before /// it reads its body, and the reservation ends when the write does. So parallel writes of one service cannot /// stage more than the service's headroom together. #[derive(Default)] pub struct ObjectStaging { /// Reserved bytes per service name. An entry is removed when its last reservation ends. reserved: Mutex>, } impl ObjectStaging { /// Reserves `bytes` for `service`, when the reservations it already holds plus `bytes` stay within /// `headroom`. The reservation ends when the guard is dropped. `None` when the budget is used up. pub fn reserve( self: &Arc, service: &str, bytes: u64, headroom: u64, ) -> Option { let mut map = self.reserved.lock().unwrap_or_else(PoisonError::into_inner); let held = map.get(service).copied().unwrap_or(0); if held.saturating_add(bytes) > headroom { return None; } map.insert(service.to_owned(), held.saturating_add(bytes)); Some(StagingGuard { staging: Arc::clone(self), service: service.to_owned(), bytes, }) } } /// A reservation of [`ObjectStaging`]. Dropping it gives the bytes back, on success and on every failure. pub struct StagingGuard { staging: Arc, service: String, bytes: u64, } impl Drop for StagingGuard { fn drop(&mut self) { let mut map = self .staging .reserved .lock() .unwrap_or_else(PoisonError::into_inner); if let Some(held) = map.get_mut(&self.service) { *held = held.saturating_sub(self.bytes); if *held == 0 { map.remove(&self.service); } } } }