move appservice_in_room_cache to service

This commit is contained in:
Charles Hall 2024-10-08 15:41:17 -07:00
parent 9d62865b28
commit 107f4521e0
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
3 changed files with 39 additions and 40 deletions

View file

@ -4,7 +4,7 @@ use std::{
io::Write, io::Write,
mem::size_of, mem::size_of,
path::Path, path::Path,
sync::{Arc, Mutex, RwLock}, sync::{Arc, Mutex},
}; };
use ruma::{ use ruma::{
@ -233,8 +233,6 @@ pub(crate) struct KeyValueDatabase {
pub(super) senderkey_pusher: Arc<dyn KvTree>, pub(super) senderkey_pusher: Arc<dyn KvTree>,
// Uncategorized trees // Uncategorized trees
pub(super) appservice_in_room_cache:
RwLock<HashMap<OwnedRoomId, HashMap<String, bool>>>,
pub(super) lasttimelinecount_cache: Mutex<HashMap<OwnedRoomId, PduCount>>, pub(super) lasttimelinecount_cache: Mutex<HashMap<OwnedRoomId, PduCount>>,
} }
@ -454,7 +452,6 @@ impl KeyValueDatabase {
global: builder.open_tree("global")?, global: builder.open_tree("global")?,
server_signingkeys: builder.open_tree("server_signingkeys")?, server_signingkeys: builder.open_tree("server_signingkeys")?,
appservice_in_room_cache: RwLock::new(HashMap::new()),
lasttimelinecount_cache: Mutex::new(HashMap::new()), lasttimelinecount_cache: Mutex::new(HashMap::new()),
}; };

View file

@ -8,7 +8,6 @@ use ruma::{
use crate::{ use crate::{
database::KeyValueDatabase, database::KeyValueDatabase,
observability::{FoundIn, Lookup, METRICS},
service::{self, appservice::RegistrationInfo}, service::{self, appservice::RegistrationInfo},
services, utils, Error, Result, services, utils, Error, Result,
}; };
@ -164,8 +163,6 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
self.serverroomids.insert(&serverroom_id, &[])?; self.serverroomids.insert(&serverroom_id, &[])?;
} }
self.appservice_in_room_cache.write().unwrap().remove(room_id);
Ok(real_users) Ok(real_users)
} }
@ -178,45 +175,22 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
room_id: &RoomId, room_id: &RoomId,
appservice: &RegistrationInfo, appservice: &RegistrationInfo,
) -> Result<bool> { ) -> Result<bool> {
let lookup = Lookup::AppserviceInRoom;
let maybe = self
.appservice_in_room_cache
.read()
.unwrap()
.get(room_id)
.and_then(|map| map.get(&appservice.registration.id))
.copied();
if let Some(b) = maybe {
METRICS.record_lookup(lookup, FoundIn::Cache);
Ok(b)
} else {
let bridge_user_id = UserId::parse_with_server_name( let bridge_user_id = UserId::parse_with_server_name(
appservice.registration.sender_localpart.as_str(), appservice.registration.sender_localpart.as_str(),
services().globals.server_name(), services().globals.server_name(),
) )
.ok(); .ok();
let in_room = bridge_user_id.map_or(false, |id| { let in_room = bridge_user_id
self.is_joined(&id, room_id).unwrap_or(false) .map_or(false, |id| self.is_joined(&id, room_id).unwrap_or(false))
}) || self.room_members(room_id).any(|userid| { || self.room_members(room_id).any(|userid| {
userid.map_or(false, |userid| { userid.map_or(false, |userid| {
appservice.users.is_match(userid.as_str()) appservice.users.is_match(userid.as_str())
}) })
}); });
METRICS.record_lookup(lookup, FoundIn::Database);
self.appservice_in_room_cache
.write()
.unwrap()
.entry(room_id.to_owned())
.or_default()
.insert(appservice.registration.id.clone(), in_room);
Ok(in_room) Ok(in_room)
} }
}
/// Makes a user forget a room. /// Makes a user forget a room.
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]

View file

@ -27,6 +27,8 @@ pub(crate) use data::Data;
pub(crate) struct Service { pub(crate) struct Service {
db: &'static dyn Data, db: &'static dyn Data,
appservice_in_room_cache:
RwLock<HashMap<OwnedRoomId, HashMap<String, bool>>>,
our_real_users_cache: our_real_users_cache:
RwLock<HashMap<OwnedRoomId, Arc<HashSet<OwnedUserId>>>>, RwLock<HashMap<OwnedRoomId, Arc<HashSet<OwnedUserId>>>>,
} }
@ -38,6 +40,7 @@ impl Service {
Self { Self {
db, db,
our_real_users_cache: RwLock::new(HashMap::new()), our_real_users_cache: RwLock::new(HashMap::new()),
appservice_in_room_cache: RwLock::new(HashMap::new()),
} }
} }
@ -309,6 +312,7 @@ impl Service {
.write() .write()
.unwrap() .unwrap()
.insert(room_id.to_owned(), our_real_users.clone()); .insert(room_id.to_owned(), our_real_users.clone());
self.appservice_in_room_cache.write().unwrap().remove(room_id);
Ok(our_real_users) Ok(our_real_users)
} }
@ -340,7 +344,31 @@ impl Service {
room_id: &RoomId, room_id: &RoomId,
appservice: &RegistrationInfo, appservice: &RegistrationInfo,
) -> Result<bool> { ) -> Result<bool> {
self.db.appservice_in_room(room_id, appservice) let lookup = Lookup::AppserviceInRoom;
if let Some(in_room) = self
.appservice_in_room_cache
.read()
.unwrap()
.get(room_id)
.and_then(|map| map.get(&appservice.registration.id))
.copied()
{
METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(in_room);
}
let in_room = self.db.appservice_in_room(room_id, appservice)?;
METRICS.record_lookup(lookup, FoundIn::Database);
self.appservice_in_room_cache
.write()
.unwrap()
.entry(room_id.to_owned())
.or_default()
.insert(appservice.registration.id.clone(), in_room);
Ok(in_room)
} }
/// Makes a user forget a room. /// Makes a user forget a room.