From 02081b66c47218b6ab3d537217727be949a46d4e Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 2 Jun 2024 00:18:57 +0000 Subject: [PATCH] Fix some unnecessary-unwraps w/ addl cleanup/simplification. Signed-off-by: Jason Volk --- src/api/client_server/sync.rs | 2 +- src/service/globals/data.rs | 18 ++++-------------- src/service/globals/mod.rs | 26 ++++++++------------------ src/service/rooms/timeline/mod.rs | 2 +- src/service/sending/mod.rs | 10 +++++----- src/service/sending/sender.rs | 6 +++--- 6 files changed, 22 insertions(+), 42 deletions(-) diff --git a/src/api/client_server/sync.rs b/src/api/client_server/sync.rs index 5925fc53..fb087489 100644 --- a/src/api/client_server/sync.rs +++ b/src/api/client_server/sync.rs @@ -142,7 +142,7 @@ pub(crate) async fn sync_events_route( .collect::>(); // Coalesce database writes for the remainder of this scope. - let _cork = services().globals.cork_and_flush()?; + let _cork = services().globals.db.cork_and_flush(); for room_id in all_joined_rooms { let room_id = room_id?; diff --git a/src/service/globals/data.rs b/src/service/globals/data.rs index f1fa621d..5b26598b 100644 --- a/src/service/globals/data.rs +++ b/src/service/globals/data.rs @@ -25,15 +25,9 @@ pub trait Data: Send + Sync { async fn watch(&self, user_id: &UserId, device_id: &DeviceId) -> Result<()>; fn cleanup(&self) -> Result<()>; - /// TODO: use this? - #[allow(dead_code)] - fn flush(&self) -> Result<()>; - fn cork(&self) -> Result; - fn cork_and_flush(&self) -> Result; + fn cork(&self) -> Cork; + fn cork_and_flush(&self) -> Cork; - /// TODO: use this? - #[allow(dead_code)] - fn cork_and_sync(&self) -> Result; fn memory_usage(&self) -> String; fn clear_caches(&self, amount: u32); fn load_keypair(&self) -> Result; @@ -177,13 +171,9 @@ impl Data for KeyValueDatabase { fn cleanup(&self) -> Result<()> { self.db.cleanup() } - fn flush(&self) -> Result<()> { self.db.flush() } + fn cork(&self) -> Cork { Cork::new(&self.db, false, false) } - fn cork(&self) -> Result { Ok(Cork::new(&self.db, false, false)) } - - fn cork_and_flush(&self) -> Result { Ok(Cork::new(&self.db, true, false)) } - - fn cork_and_sync(&self) -> Result { Ok(Cork::new(&self.db, true, true)) } + fn cork_and_flush(&self) -> Cork { Cork::new(&self.db, true, false) } fn memory_usage(&self) -> String { let auth_chain_cache = self.auth_chain_cache.lock().unwrap().len(); diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 723979da..2621ae24 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -1,3 +1,10 @@ +mod client; +mod data; +pub(super) mod emerg_access; +pub(super) mod migrations; +mod resolver; +pub(super) mod updates; + use std::{ collections::{BTreeMap, HashMap}, fs, @@ -29,14 +36,7 @@ use tokio::{ use tracing::{error, trace}; use url::Url; -use crate::{database::Cork, services, Config, Result}; - -mod client; -mod data; -pub(crate) mod emerg_access; -pub(crate) mod migrations; -mod resolver; -pub(crate) mod updates; +use crate::{services, Config, Result}; type RateLimitState = (Instant, u32); // Time if last failed try, number of failed tries @@ -194,16 +194,6 @@ impl Service { self.db.watch(user_id, device_id).await } - pub fn cleanup(&self) -> Result<()> { self.db.cleanup() } - - /// TODO: use this? - #[allow(dead_code)] - pub fn flush(&self) -> Result<()> { self.db.flush() } - - pub fn cork(&self) -> Result { self.db.cork() } - - pub fn cork_and_flush(&self) -> Result { self.db.cork_and_flush() } - pub fn server_name(&self) -> &ServerName { self.config.server_name.as_ref() } pub fn max_request_size(&self) -> u32 { self.config.max_request_size } diff --git a/src/service/rooms/timeline/mod.rs b/src/service/rooms/timeline/mod.rs index 4d91375f..04f03242 100644 --- a/src/service/rooms/timeline/mod.rs +++ b/src/service/rooms/timeline/mod.rs @@ -195,7 +195,7 @@ impl Service { state_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room state mutex ) -> Result> { // Coalesce database writes for the remainder of this scope. - let _cork = services().globals.cork_and_flush()?; + let _cork = services().globals.db.cork_and_flush(); let shortroomid = services() .rooms diff --git a/src/service/sending/mod.rs b/src/service/sending/mod.rs index a9f64f7b..654ec523 100644 --- a/src/service/sending/mod.rs +++ b/src/service/sending/mod.rs @@ -81,7 +81,7 @@ impl Service { pub fn send_pdu_push(&self, pdu_id: &[u8], user: &UserId, pushkey: String) -> Result<()> { let dest = Destination::Push(user.to_owned(), pushkey); let event = SendingEvent::Pdu(pdu_id.to_owned()); - let _cork = services().globals.cork()?; + let _cork = services().globals.db.cork(); let keys = self.db.queue_requests(&[(&dest, event.clone())])?; self.dispatch(Msg { dest, @@ -94,7 +94,7 @@ impl Service { pub fn send_pdu_appservice(&self, appservice_id: String, pdu_id: Vec) -> Result<()> { let dest = Destination::Appservice(appservice_id); let event = SendingEvent::Pdu(pdu_id); - let _cork = services().globals.cork()?; + let _cork = services().globals.db.cork(); let keys = self.db.queue_requests(&[(&dest, event.clone())])?; self.dispatch(Msg { dest, @@ -121,7 +121,7 @@ impl Service { .into_iter() .map(|server| (Destination::Normal(server), SendingEvent::Pdu(pdu_id.to_owned()))) .collect::>(); - let _cork = services().globals.cork()?; + let _cork = services().globals.db.cork(); let keys = self.db.queue_requests( &requests .iter() @@ -143,7 +143,7 @@ impl Service { pub fn send_edu_server(&self, server: &ServerName, serialized: Vec) -> Result<()> { let dest = Destination::Normal(server.to_owned()); let event = SendingEvent::Edu(serialized); - let _cork = services().globals.cork()?; + let _cork = services().globals.db.cork(); let keys = self.db.queue_requests(&[(&dest, event.clone())])?; self.dispatch(Msg { dest, @@ -170,7 +170,7 @@ impl Service { .into_iter() .map(|server| (Destination::Normal(server), SendingEvent::Edu(serialized.clone()))) .collect::>(); - let _cork = services().globals.cork()?; + let _cork = services().globals.db.cork(); let keys = self.db.queue_requests( &requests .iter() diff --git a/src/service/sending/sender.rs b/src/service/sending/sender.rs index 8bb93105..3d1d89da 100644 --- a/src/service/sending/sender.rs +++ b/src/service/sending/sender.rs @@ -100,7 +100,7 @@ impl Service { fn handle_response_ok( &self, dest: &Destination, futures: &mut SendingFutures<'_>, statuses: &mut CurTransactionStatus, ) { - let _cork = services().globals.cork(); + let _cork = services().globals.db.cork(); self.db .delete_all_active_requests_for(dest) .expect("all active requests deleted"); @@ -173,7 +173,7 @@ impl Service { return Ok(None); } - let _cork = services().globals.cork(); + let _cork = services().globals.db.cork(); let mut events = Vec::new(); // Must retry any previous transaction for this remote. @@ -187,7 +187,7 @@ impl Service { } // Compose the next transaction - let _cork = services().globals.cork(); + let _cork = services().globals.db.cork(); if !new_events.is_empty() { self.db.mark_as_active(&new_events)?; for (e, _) in new_events {