Fix some unnecessary-unwraps w/ addl cleanup/simplification.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-02 00:18:57 +00:00 committed by June 🍓🦴
parent b3fc8516ed
commit 02081b66c4
6 changed files with 22 additions and 42 deletions

View file

@ -142,7 +142,7 @@ pub(crate) async fn sync_events_route(
.collect::<Vec<_>>();
// 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?;

View file

@ -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<Cork>;
fn cork_and_flush(&self) -> Result<Cork>;
fn cork(&self) -> Cork;
fn cork_and_flush(&self) -> Cork;
/// TODO: use this?
#[allow(dead_code)]
fn cork_and_sync(&self) -> Result<Cork>;
fn memory_usage(&self) -> String;
fn clear_caches(&self, amount: u32);
fn load_keypair(&self) -> Result<Ed25519KeyPair>;
@ -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<Cork> { Ok(Cork::new(&self.db, false, false)) }
fn cork_and_flush(&self) -> Result<Cork> { Ok(Cork::new(&self.db, true, false)) }
fn cork_and_sync(&self) -> Result<Cork> { 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();

View file

@ -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<Cork> { self.db.cork() }
pub fn cork_and_flush(&self) -> Result<Cork> { 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 }

View file

@ -195,7 +195,7 @@ impl Service {
state_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room state mutex
) -> Result<Vec<u8>> {
// 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

View file

@ -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<u8>) -> 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::<Vec<_>>();
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<u8>) -> 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::<Vec<_>>();
let _cork = services().globals.cork()?;
let _cork = services().globals.db.cork();
let keys = self.db.queue_requests(
&requests
.iter()

View file

@ -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 {