use OnceLock instead of RwLock for SERVICES

It actually has the semantics we need. Until we get rid of SERVICES.
This commit is contained in:
Charles Hall 2024-09-24 20:05:18 -07:00
parent 032e1ca3c6
commit ad37eae869
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF

View file

@ -1,6 +1,6 @@
use std::{
collections::{BTreeMap, HashMap},
sync::{Arc, Mutex as StdMutex, RwLock as StdRwLock},
sync::{Arc, Mutex as StdMutex, OnceLock},
};
use lru_cache::LruCache;
@ -22,14 +22,11 @@ pub(crate) mod transaction_ids;
pub(crate) mod uiaa;
pub(crate) mod users;
static SERVICES: StdRwLock<Option<&'static Services>> = StdRwLock::new(None);
static SERVICES: OnceLock<&'static Services> = OnceLock::new();
/// Convenient access to the global [`Services`] instance
pub(crate) fn services() -> &'static Services {
SERVICES
.read()
.unwrap()
.expect("SERVICES should be initialized when this is called")
SERVICES.get().expect("`Services::install` should have been called first")
}
pub(crate) struct Services {
@ -166,7 +163,10 @@ impl Services {
/// Installs `self` to be globally accessed via [`services`]
pub(crate) fn install(self) {
*SERVICES.write().unwrap() = Some(Box::leak(Box::new(self)));
assert!(
SERVICES.set(Box::leak(Box::new(self))).is_ok(),
"Services::install was called more than once"
);
}
async fn memory_usage(&self) -> String {