forge
Explore
Log in

Don't you dare go Hollow

in

Clone
★ 0

0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits

state.rs97 lines, 3.6 KiBHistoryRawDownload
1//! The state shared by every route. Topcoat hands it to each handler through the app context.
2
3use std::collections::HashMap;
4use std::sync::{Arc, Mutex, PoisonError};
5
6use in_core::blob::BlobStore;
7use in_core::config::Config;
8use in_core::store::Store;
9
10use crate::backup::BackupTrigger;
11use crate::family::FamilyCache;
12use crate::live::LiveHub;
13use crate::oidc::OidcClient;
14use crate::throttle::ShareThrottles;
15
16/// Everything a request handler reaches through the app context. Every field is cheap to clone.
17#[allow(dead_code)] // The live hub is read by the live pages, which come after sign-in.
18#[derive(Clone)]
19pub struct AppState {
20 /// The configuration the process booted with.
21 pub config: Arc<Config>,
22 /// The database. Reads go to a pooled reader and writes to the single writer.
23 pub store: Store,
24 /// The blob store the configuration names: the local blob tree or an S3-compatible bucket.
25 pub blobs: Arc<dyn BlobStore>,
26 /// The instance key (`in.key`). Never used directly: every seal and MAC uses a subkey derived from it (see
27 /// `in_core::secret`).
28 pub key: Arc<[u8; 32]>,
29 /// The change counters of the live pages.
30 pub live: Arc<LiveHub>,
31 /// Asks the backup worker for a run now (the admin page). Shared with the worker.
32 pub backup: Arc<BackupTrigger>,
33 /// The OpenID Connect client of `im`. `None` when the configuration has no `[oidc]` table, in which
34 /// case nobody can sign in and the sign-in routes say so.
35 pub oidc: Option<Arc<OidcClient>>,
36 /// The failure windows of share-link passwords.
37 pub throttles: Arc<ShareThrottles>,
38 /// The switcher list of the family, refreshed by the family worker.
39 pub family: Arc<FamilyCache>,
40 /// The bytes object writes may stage right now, per service.
41 pub staging: Arc<ObjectStaging>,
42}
43
44/// Bytes of object writes that are being staged, per service. A write reserves the bytes it may stage before
45/// it reads its body, and the reservation ends when the write does. So parallel writes of one service cannot
46/// stage more than the service's headroom together.
47#[derive(Default)]
48pub struct ObjectStaging {
49 /// Reserved bytes per service name. An entry is removed when its last reservation ends.
50 reserved: Mutex<HashMap<String, u64>>,
51}
52
53impl ObjectStaging {
54 /// Reserves `bytes` for `service`, when the reservations it already holds plus `bytes` stay within
55 /// `headroom`. The reservation ends when the guard is dropped. `None` when the budget is used up.
56 pub fn reserve(
57 self: &Arc<Self>,
58 service: &str,
59 bytes: u64,
60 headroom: u64,
61 ) -> Option<StagingGuard> {
62 let mut map = self.reserved.lock().unwrap_or_else(PoisonError::into_inner);
63 let held = map.get(service).copied().unwrap_or(0);
64 if held.saturating_add(bytes) > headroom {
65 return None;
66 }
67 map.insert(service.to_owned(), held.saturating_add(bytes));
68 Some(StagingGuard {
69 staging: Arc::clone(self),
70 service: service.to_owned(),
71 bytes,
72 })
73 }
74}
75
76/// A reservation of [`ObjectStaging`]. Dropping it gives the bytes back, on success and on every failure.
77pub struct StagingGuard {
78 staging: Arc<ObjectStaging>,
79 service: String,
80 bytes: u64,
81}
82
83impl Drop for StagingGuard {
84 fn drop(&mut self) {
85 let mut map = self
86 .staging
87 .reserved
88 .lock()
89 .unwrap_or_else(PoisonError::into_inner);
90 if let Some(held) = map.get_mut(&self.service) {
91 *held = held.saturating_sub(self.bytes);
92 if *held == 0 {
93 map.remove(&self.service);
94 }
95 }
96 }
97}