hide global services jank in service module

Mainly to make it easier to initialize the SERVICES global correctly in
more than one place.

Also this stuff really shouldn't live at the crate root anyway.
This commit is contained in:
Charles Hall 2024-09-24 19:48:33 -07:00
parent 1fd20cdeba
commit 032e1ca3c6
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
3 changed files with 22 additions and 20 deletions

View file

@ -47,7 +47,7 @@ use crate::{
self, self,
error::{Error, Result}, error::{Error, Result},
}, },
Services, SERVICES, Services,
}; };
pub(crate) async fn run(args: ServeArgs) -> Result<(), error::ServeCommand> { 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)?, .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) Services::build(db, config, reload_handles)
.map_err(Error::InitializeServices)?, .map_err(Error::InitializeServices)?
))); .install();
services().globals.err_if_server_name_changed()?; services().globals.err_if_server_name_changed()?;

View file

@ -2,7 +2,7 @@
// work anyway // work anyway
#![cfg_attr(not(any(feature = "sqlite", feature = "rocksdb")), allow(unused))] #![cfg_attr(not(any(feature = "sqlite", feature = "rocksdb")), allow(unused))]
use std::{process::ExitCode, sync::RwLock}; use std::process::ExitCode;
use clap::Parser; use clap::Parser;
use tracing::error; use tracing::error;
@ -18,7 +18,7 @@ mod utils;
pub(crate) use api::ruma_wrapper::{Ar, Ra}; pub(crate) use api::ruma_wrapper::{Ar, Ra};
pub(crate) use config::Config; 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"))] #[cfg(all(not(target_env = "msvc"), feature = "jemalloc"))]
use tikv_jemallocator::Jemalloc; use tikv_jemallocator::Jemalloc;
pub(crate) use utils::error::{Error, Result}; pub(crate) use utils::error::{Error, Result};
@ -27,17 +27,6 @@ pub(crate) use utils::error::{Error, Result};
#[global_allocator] #[global_allocator]
static GLOBAL: Jemalloc = Jemalloc; static GLOBAL: Jemalloc = Jemalloc;
pub(crate) static SERVICES: RwLock<Option<&'static Services>> =
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 /// Returns the current version of the crate with extra info if supplied
/// ///
/// Set the environment variable `GRAPEVINE_VERSION_EXTRA` to any UTF-8 string /// Set the environment variable `GRAPEVINE_VERSION_EXTRA` to any UTF-8 string

View file

@ -1,6 +1,6 @@
use std::{ use std::{
collections::{BTreeMap, HashMap}, collections::{BTreeMap, HashMap},
sync::{Arc, Mutex as StdMutex}, sync::{Arc, Mutex as StdMutex, RwLock as StdRwLock},
}; };
use lru_cache::LruCache; use lru_cache::LruCache;
@ -22,6 +22,16 @@ pub(crate) mod transaction_ids;
pub(crate) mod uiaa; pub(crate) mod uiaa;
pub(crate) mod users; pub(crate) mod users;
static SERVICES: StdRwLock<Option<&'static Services>> = 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) struct Services {
pub(crate) appservice: appservice::Service, pub(crate) appservice: appservice::Service,
pub(crate) pusher: pusher::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 { async fn memory_usage(&self) -> String {
let lazy_load_waiting = let lazy_load_waiting =
self.rooms.lazy_loading.lazy_load_waiting.lock().await.len(); self.rooms.lazy_loading.lazy_load_waiting.lock().await.len();