diff --git a/src/cli/serve.rs b/src/cli/serve.rs index ab65396b..79a55482 100644 --- a/src/cli/serve.rs +++ b/src/cli/serve.rs @@ -47,7 +47,7 @@ use crate::{ self, error::{Error, Result}, }, - Services, SERVICES, + Services, }; pub(crate) async fn run(args: ServeArgs) -> Result<(), error::ServeCommand> { @@ -78,11 +78,9 @@ pub(crate) async fn run(args: ServeArgs) -> Result<(), error::ServeCommand> { .map_err(Error::DatabaseError)?, )); - // This is the first and only time we initialize the SERVICE static - *SERVICES.write().unwrap() = Some(Box::leak(Box::new( - Services::build(db, config, reload_handles) - .map_err(Error::InitializeServices)?, - ))); + Services::build(db, config, reload_handles) + .map_err(Error::InitializeServices)? + .install(); services().globals.err_if_server_name_changed()?; diff --git a/src/main.rs b/src/main.rs index 080b9714..903906a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ // work anyway #![cfg_attr(not(any(feature = "sqlite", feature = "rocksdb")), allow(unused))] -use std::{process::ExitCode, sync::RwLock}; +use std::process::ExitCode; use clap::Parser; use tracing::error; @@ -18,7 +18,7 @@ mod utils; pub(crate) use api::ruma_wrapper::{Ar, Ra}; pub(crate) use config::Config; -pub(crate) use service::{pdu::PduEvent, Services}; +pub(crate) use service::{pdu::PduEvent, services, Services}; #[cfg(all(not(target_env = "msvc"), feature = "jemalloc"))] use tikv_jemallocator::Jemalloc; pub(crate) use utils::error::{Error, Result}; @@ -27,17 +27,6 @@ pub(crate) use utils::error::{Error, Result}; #[global_allocator] static GLOBAL: Jemalloc = Jemalloc; -pub(crate) static SERVICES: RwLock> = - RwLock::new(None); - -/// 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") -} - /// Returns the current version of the crate with extra info if supplied /// /// Set the environment variable `GRAPEVINE_VERSION_EXTRA` to any UTF-8 string diff --git a/src/service.rs b/src/service.rs index 61216a61..4bf7c5e2 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,6 +1,6 @@ use std::{ collections::{BTreeMap, HashMap}, - sync::{Arc, Mutex as StdMutex}, + sync::{Arc, Mutex as StdMutex, RwLock as StdRwLock}, }; use lru_cache::LruCache; @@ -22,6 +22,16 @@ pub(crate) mod transaction_ids; pub(crate) mod uiaa; pub(crate) mod users; +static SERVICES: StdRwLock> = StdRwLock::new(None); + +/// 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") +} + pub(crate) struct Services { pub(crate) appservice: appservice::Service, pub(crate) pusher: pusher::Service, @@ -154,6 +164,11 @@ impl Services { }) } + /// Installs `self` to be globally accessed via [`services`] + pub(crate) fn install(self) { + *SERVICES.write().unwrap() = Some(Box::leak(Box::new(self))); + } + async fn memory_usage(&self) -> String { let lazy_load_waiting = self.rooms.lazy_loading.lazy_load_waiting.lock().await.len();