1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-06-27 16:35:59 +00:00
conduit/src/service/mod.rs

221 lines
7 KiB
Rust
Raw Normal View History

2022-10-05 18:36:12 +02:00
use std::{
2023-07-23 21:57:11 +02:00
collections::{BTreeMap, HashMap},
sync::{Arc, Mutex as StdMutex},
2022-10-05 18:36:12 +02:00
};
2022-10-05 20:33:55 +02:00
use lru_cache::LruCache;
use tokio::sync::{broadcast, Mutex};
2022-10-05 20:33:55 +02:00
2022-10-05 20:34:31 +02:00
use crate::{Config, Result};
use tokio::sync::RwLock;
2022-10-05 12:45:54 +02:00
pub mod account_data;
pub mod admin;
2022-09-07 13:25:51 +02:00
pub mod appservice;
pub mod globals;
pub mod key_backups;
pub mod media;
2022-09-07 13:25:51 +02:00
pub mod pdu;
pub mod pusher;
pub mod rooms;
pub mod sending;
2022-09-07 13:25:51 +02:00
pub mod transaction_ids;
pub mod uiaa;
pub mod users;
2022-10-05 12:45:54 +02:00
pub struct Services {
pub appservice: appservice::Service,
pub pusher: pusher::Service,
pub rooms: rooms::Service,
pub transaction_ids: transaction_ids::Service,
pub uiaa: uiaa::Service,
pub users: users::Service,
pub account_data: account_data::Service,
2022-10-08 13:02:52 +02:00
pub admin: Arc<admin::Service>,
2022-10-05 12:45:54 +02:00
pub globals: globals::Service,
pub key_backups: key_backups::Service,
pub media: media::Service,
2022-10-08 13:02:52 +02:00
pub sending: Arc<sending::Service>,
}
2022-10-05 12:45:54 +02:00
impl Services {
2022-10-05 18:36:12 +02:00
pub fn build<
D: appservice::Data
+ pusher::Data
+ rooms::Data
+ transaction_ids::Data
+ uiaa::Data
+ users::Data
+ account_data::Data
+ globals::Data
+ key_backups::Data
2022-10-08 13:02:52 +02:00
+ media::Data
+ sending::Data
2022-10-08 13:03:07 +02:00
+ 'static,
2022-10-05 18:36:12 +02:00
>(
2022-10-08 13:02:52 +02:00
db: &'static D,
2022-10-05 20:34:31 +02:00
config: Config,
2022-10-05 18:36:12 +02:00
) -> Result<Self> {
Ok(Self {
appservice: appservice::Service::build(db)?,
2022-10-08 13:02:52 +02:00
pusher: pusher::Service { db },
2022-10-05 18:36:12 +02:00
rooms: rooms::Service {
2022-10-08 13:02:52 +02:00
alias: rooms::alias::Service { db },
auth_chain: rooms::auth_chain::Service { db },
directory: rooms::directory::Service { db },
2022-10-05 18:36:12 +02:00
edus: rooms::edus::Service {
2022-10-08 13:02:52 +02:00
presence: rooms::edus::presence::Service { db },
read_receipt: rooms::edus::read_receipt::Service { db },
typing: rooms::edus::typing::Service {
typing: RwLock::new(BTreeMap::new()),
last_typing_update: RwLock::new(BTreeMap::new()),
typing_update_sender: broadcast::channel(100).0,
},
2022-10-05 18:36:12 +02:00
},
event_handler: rooms::event_handler::Service,
lazy_loading: rooms::lazy_loading::Service {
2022-10-08 13:02:52 +02:00
db,
2022-10-05 18:36:12 +02:00
lazy_load_waiting: Mutex::new(HashMap::new()),
},
2022-10-08 13:02:52 +02:00
metadata: rooms::metadata::Service { db },
outlier: rooms::outlier::Service { db },
pdu_metadata: rooms::pdu_metadata::Service { db },
search: rooms::search::Service { db },
short: rooms::short::Service { db },
state: rooms::state::Service { db },
state_accessor: rooms::state_accessor::Service {
db,
server_visibility_cache: StdMutex::new(LruCache::new(
(100.0 * config.conduit_cache_capacity_modifier) as usize,
)),
user_visibility_cache: StdMutex::new(LruCache::new(
2023-02-22 15:49:55 +01:00
(100.0 * config.conduit_cache_capacity_modifier) as usize,
)),
},
2022-10-08 13:02:52 +02:00
state_cache: rooms::state_cache::Service { db },
2022-10-05 20:34:31 +02:00
state_compressor: rooms::state_compressor::Service {
2022-10-08 13:02:52 +02:00
db,
stateinfo_cache: StdMutex::new(LruCache::new(
(100.0 * config.conduit_cache_capacity_modifier) as usize,
2022-10-05 20:34:31 +02:00
)),
},
timeline: rooms::timeline::Service {
2022-10-08 13:02:52 +02:00
db,
2022-10-05 20:34:31 +02:00
lasttimelinecount_cache: Mutex::new(HashMap::new()),
},
2023-06-25 19:31:40 +02:00
threads: rooms::threads::Service { db },
2023-07-02 16:06:54 +02:00
spaces: rooms::spaces::Service {
roomid_spacechunk_cache: Mutex::new(LruCache::new(200)),
},
2022-10-08 13:02:52 +02:00
user: rooms::user::Service { db },
2022-10-05 18:36:12 +02:00
},
2022-10-08 13:02:52 +02:00
transaction_ids: transaction_ids::Service { db },
uiaa: uiaa::Service { db },
2023-07-23 21:57:11 +02:00
users: users::Service {
db,
connections: StdMutex::new(BTreeMap::new()),
2023-07-23 21:57:11 +02:00
},
2022-10-08 13:02:52 +02:00
account_data: account_data::Service { db },
admin: admin::Service::build(),
key_backups: key_backups::Service { db },
media: media::Service { db },
sending: sending::Service::build(db, &config),
globals: globals::Service::load(db, config)?,
2022-10-05 18:36:12 +02:00
})
2022-10-05 12:45:54 +02:00
}
async fn memory_usage(&self) -> String {
let lazy_load_waiting = self.rooms.lazy_loading.lazy_load_waiting.lock().await.len();
let server_visibility_cache = self
.rooms
.state_accessor
.server_visibility_cache
.lock()
.unwrap()
.len();
let user_visibility_cache = self
.rooms
.state_accessor
.user_visibility_cache
.lock()
.unwrap()
.len();
let stateinfo_cache = self
.rooms
.state_compressor
.stateinfo_cache
.lock()
.unwrap()
.len();
let lasttimelinecount_cache = self
.rooms
.timeline
.lasttimelinecount_cache
.lock()
.await
.len();
let roomid_spacechunk_cache = self.rooms.spaces.roomid_spacechunk_cache.lock().await.len();
format!(
"\
lazy_load_waiting: {lazy_load_waiting}
server_visibility_cache: {server_visibility_cache}
user_visibility_cache: {user_visibility_cache}
stateinfo_cache: {stateinfo_cache}
lasttimelinecount_cache: {lasttimelinecount_cache}
roomid_spacechunk_cache: {roomid_spacechunk_cache}\
"
)
}
async fn clear_caches(&self, amount: u32) {
if amount > 0 {
self.rooms
.lazy_loading
.lazy_load_waiting
.lock()
.await
.clear();
}
if amount > 1 {
self.rooms
.state_accessor
.server_visibility_cache
.lock()
.unwrap()
.clear();
}
if amount > 2 {
self.rooms
.state_accessor
.user_visibility_cache
.lock()
.unwrap()
.clear();
}
if amount > 3 {
self.rooms
.state_compressor
.stateinfo_cache
.lock()
.unwrap()
.clear();
}
if amount > 4 {
self.rooms
.timeline
.lasttimelinecount_cache
.lock()
.await
.clear();
}
if amount > 5 {
self.rooms
.spaces
.roomid_spacechunk_cache
.lock()
.await
.clear();
}
}
2022-10-05 12:45:54 +02:00
}