From 395b466b4ab19d24d506813d18be7c627ffad3d1 Mon Sep 17 00:00:00 2001 From: strawberry Date: Wed, 17 Apr 2024 20:11:18 -0400 Subject: [PATCH] rename OutgoingKind enum to OutgoingDestination Signed-off-by: strawberry --- src/database/key_value/sending.rs | 22 +++++------ src/service/sending/data.rs | 14 +++---- src/service/sending/mod.rs | 65 ++++++++++++++++--------------- 3 files changed, 52 insertions(+), 49 deletions(-) diff --git a/src/database/key_value/sending.rs b/src/database/key_value/sending.rs index 9f871ee7..78dfd197 100644 --- a/src/database/key_value/sending.rs +++ b/src/database/key_value/sending.rs @@ -4,7 +4,7 @@ use crate::{ database::KeyValueDatabase, service::{ self, - sending::{OutgoingKind, SendingEventType}, + sending::{OutgoingDestination, SendingEventType}, }, services, utils, Error, Result, }; @@ -12,7 +12,7 @@ use crate::{ impl service::sending::Data for KeyValueDatabase { fn active_requests<'a>( &'a self, - ) -> Box, OutgoingKind, SendingEventType)>> + 'a> { + ) -> Box, OutgoingDestination, SendingEventType)>> + 'a> { Box::new( self.servercurrentevent_data .iter() @@ -21,7 +21,7 @@ impl service::sending::Data for KeyValueDatabase { } fn active_requests_for<'a>( - &'a self, outgoing_kind: &OutgoingKind, + &'a self, outgoing_kind: &OutgoingDestination, ) -> Box, SendingEventType)>> + 'a> { let prefix = outgoing_kind.get_prefix(); Box::new( @@ -33,7 +33,7 @@ impl service::sending::Data for KeyValueDatabase { fn delete_active_request(&self, key: Vec) -> Result<()> { self.servercurrentevent_data.remove(&key) } - fn delete_all_active_requests_for(&self, outgoing_kind: &OutgoingKind) -> Result<()> { + fn delete_all_active_requests_for(&self, outgoing_kind: &OutgoingDestination) -> Result<()> { let prefix = outgoing_kind.get_prefix(); for (key, _) in self.servercurrentevent_data.scan_prefix(prefix) { self.servercurrentevent_data.remove(&key)?; @@ -42,7 +42,7 @@ impl service::sending::Data for KeyValueDatabase { Ok(()) } - fn delete_all_requests_for(&self, outgoing_kind: &OutgoingKind) -> Result<()> { + fn delete_all_requests_for(&self, outgoing_kind: &OutgoingDestination) -> Result<()> { let prefix = outgoing_kind.get_prefix(); for (key, _) in self.servercurrentevent_data.scan_prefix(prefix.clone()) { self.servercurrentevent_data.remove(&key).unwrap(); @@ -55,7 +55,7 @@ impl service::sending::Data for KeyValueDatabase { Ok(()) } - fn queue_requests(&self, requests: &[(&OutgoingKind, SendingEventType)]) -> Result>> { + fn queue_requests(&self, requests: &[(&OutgoingDestination, SendingEventType)]) -> Result>> { let mut batch = Vec::new(); let mut keys = Vec::new(); for (outgoing_kind, event) in requests { @@ -79,7 +79,7 @@ impl service::sending::Data for KeyValueDatabase { } fn queued_requests<'a>( - &'a self, outgoing_kind: &OutgoingKind, + &'a self, outgoing_kind: &OutgoingDestination, ) -> Box)>> + 'a> { let prefix = outgoing_kind.get_prefix(); return Box::new( @@ -122,7 +122,7 @@ impl service::sending::Data for KeyValueDatabase { } #[tracing::instrument(skip(key))] -fn parse_servercurrentevent(key: &[u8], value: Vec) -> Result<(OutgoingKind, SendingEventType)> { +fn parse_servercurrentevent(key: &[u8], value: Vec) -> Result<(OutgoingDestination, SendingEventType)> { // Appservices start with a plus Ok::<_, Error>(if key.starts_with(b"+") { let mut parts = key[1..].splitn(2, |&b| b == 0xFF); @@ -136,7 +136,7 @@ fn parse_servercurrentevent(key: &[u8], value: Vec) -> Result<(OutgoingKind, .map_err(|_| Error::bad_database("Invalid server bytes in server_currenttransaction"))?; ( - OutgoingKind::Appservice(server), + OutgoingDestination::Appservice(server), if value.is_empty() { SendingEventType::Pdu(event.to_vec()) } else { @@ -163,7 +163,7 @@ fn parse_servercurrentevent(key: &[u8], value: Vec) -> Result<(OutgoingKind, .ok_or_else(|| Error::bad_database("Invalid bytes in servercurrentpdus."))?; ( - OutgoingKind::Push(user_id, pushkey_string), + OutgoingDestination::Push(user_id, pushkey_string), if value.is_empty() { SendingEventType::Pdu(event.to_vec()) } else { @@ -183,7 +183,7 @@ fn parse_servercurrentevent(key: &[u8], value: Vec) -> Result<(OutgoingKind, .map_err(|_| Error::bad_database("Invalid server bytes in server_currenttransaction"))?; ( - OutgoingKind::Normal( + OutgoingDestination::Normal( ServerName::parse(server) .map_err(|_| Error::bad_database("Invalid server string in server_currenttransaction"))?, ), diff --git a/src/service/sending/data.rs b/src/service/sending/data.rs index 46f3cd71..d5b0923e 100644 --- a/src/service/sending/data.rs +++ b/src/service/sending/data.rs @@ -1,20 +1,20 @@ use ruma::ServerName; -use super::{OutgoingKind, SendingEventType}; +use super::{OutgoingDestination, SendingEventType}; use crate::Result; -type OutgoingSendingIter<'a> = Box, OutgoingKind, SendingEventType)>> + 'a>; +type OutgoingSendingIter<'a> = Box, OutgoingDestination, SendingEventType)>> + 'a>; type SendingEventTypeIter<'a> = Box, SendingEventType)>> + 'a>; pub trait Data: Send + Sync { fn active_requests(&self) -> OutgoingSendingIter<'_>; - fn active_requests_for(&self, outgoing_kind: &OutgoingKind) -> SendingEventTypeIter<'_>; + fn active_requests_for(&self, outgoing_kind: &OutgoingDestination) -> SendingEventTypeIter<'_>; fn delete_active_request(&self, key: Vec) -> Result<()>; - fn delete_all_active_requests_for(&self, outgoing_kind: &OutgoingKind) -> Result<()>; - fn delete_all_requests_for(&self, outgoing_kind: &OutgoingKind) -> Result<()>; - fn queue_requests(&self, requests: &[(&OutgoingKind, SendingEventType)]) -> Result>>; + fn delete_all_active_requests_for(&self, outgoing_kind: &OutgoingDestination) -> Result<()>; + fn delete_all_requests_for(&self, outgoing_kind: &OutgoingDestination) -> Result<()>; + fn queue_requests(&self, requests: &[(&OutgoingDestination, SendingEventType)]) -> Result>>; fn queued_requests<'a>( - &'a self, outgoing_kind: &OutgoingKind, + &'a self, outgoing_kind: &OutgoingDestination, ) -> Box)>> + 'a>; fn mark_as_active(&self, events: &[(SendingEventType, Vec)]) -> Result<()>; fn set_latest_educount(&self, server_name: &ServerName, educount: u64) -> Result<()>; diff --git a/src/service/sending/mod.rs b/src/service/sending/mod.rs index 8c813970..15dfd74b 100644 --- a/src/service/sending/mod.rs +++ b/src/service/sending/mod.rs @@ -42,15 +42,15 @@ pub struct Service { /// The state for a given state hash. pub(super) maximum_requests: Arc, - pub sender: loole::Sender<(OutgoingKind, SendingEventType, Vec)>, - receiver: Mutex)>>, + pub sender: loole::Sender<(OutgoingDestination, SendingEventType, Vec)>, + receiver: Mutex)>>, startup_netburst: bool, startup_netburst_keep: i64, timeout: u64, } #[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub enum OutgoingKind { +pub enum OutgoingDestination { Appservice(String), Push(OwnedUserId, String), // user and pushkey Normal(OwnedServerName), @@ -86,7 +86,7 @@ impl Service { #[tracing::instrument(skip(self, pdu_id, user, pushkey))] pub fn send_pdu_push(&self, pdu_id: &[u8], user: &UserId, pushkey: String) -> Result<()> { - let outgoing_kind = OutgoingKind::Push(user.to_owned(), pushkey); + let outgoing_kind = OutgoingDestination::Push(user.to_owned(), pushkey); let event = SendingEventType::Pdu(pdu_id.to_owned()); let _cork = services().globals.db.cork()?; let keys = self.db.queue_requests(&[(&outgoing_kind, event.clone())])?; @@ -99,7 +99,7 @@ impl Service { #[tracing::instrument(skip(self))] pub fn send_pdu_appservice(&self, appservice_id: String, pdu_id: Vec) -> Result<()> { - let outgoing_kind = OutgoingKind::Appservice(appservice_id); + let outgoing_kind = OutgoingDestination::Appservice(appservice_id); let event = SendingEventType::Pdu(pdu_id); let _cork = services().globals.db.cork()?; let keys = self.db.queue_requests(&[(&outgoing_kind, event.clone())])?; @@ -126,7 +126,7 @@ impl Service { pub fn send_pdu_servers>(&self, servers: I, pdu_id: &[u8]) -> Result<()> { let requests = servers .into_iter() - .map(|server| (OutgoingKind::Normal(server), SendingEventType::Pdu(pdu_id.to_owned()))) + .map(|server| (OutgoingDestination::Normal(server), SendingEventType::Pdu(pdu_id.to_owned()))) .collect::>(); let _cork = services().globals.db.cork()?; let keys = self.db.queue_requests( @@ -146,7 +146,7 @@ impl Service { #[tracing::instrument(skip(self, server, serialized))] pub fn send_edu_server(&self, server: &ServerName, serialized: Vec) -> Result<()> { - let outgoing_kind = OutgoingKind::Normal(server.to_owned()); + let outgoing_kind = OutgoingDestination::Normal(server.to_owned()); let event = SendingEventType::Edu(serialized); let _cork = services().globals.db.cork()?; let keys = self.db.queue_requests(&[(&outgoing_kind, event.clone())])?; @@ -173,7 +173,7 @@ impl Service { pub fn send_edu_servers>(&self, servers: I, serialized: Vec) -> Result<()> { let requests = servers .into_iter() - .map(|server| (OutgoingKind::Normal(server), SendingEventType::Edu(serialized.clone()))) + .map(|server| (OutgoingDestination::Normal(server), SendingEventType::Edu(serialized.clone()))) .collect::>(); let _cork = services().globals.db.cork()?; let keys = self.db.queue_requests( @@ -205,7 +205,7 @@ impl Service { #[tracing::instrument(skip(self, servers))] pub fn flush_servers>(&self, servers: I) -> Result<()> { - let requests = servers.into_iter().map(OutgoingKind::Normal); + let requests = servers.into_iter().map(OutgoingDestination::Normal); for outgoing_kind in requests { self.sender @@ -221,7 +221,7 @@ impl Service { #[tracing::instrument(skip(self))] pub fn cleanup_events(&self, appservice_id: String) -> Result<()> { self.db - .delete_all_requests_for(&OutgoingKind::Appservice(appservice_id))?; + .delete_all_requests_for(&OutgoingDestination::Appservice(appservice_id))?; Ok(()) } @@ -277,11 +277,11 @@ impl Service { let receiver = self.receiver.lock().await; let mut futures = FuturesUnordered::new(); - let mut current_transaction_status = HashMap::::new(); + let mut current_transaction_status = HashMap::::new(); // Retry requests we could not finish yet if self.startup_netburst { - let mut initial_transactions = HashMap::>::new(); + let mut initial_transactions = HashMap::>::new(); for (key, outgoing_kind, event) in self.db.active_requests().filter_map(Result::ok) { let entry = initial_transactions .entry(outgoing_kind.clone()) @@ -361,9 +361,9 @@ impl Service { #[tracing::instrument(skip(self, outgoing_kind, new_events, current_transaction_status))] fn select_events( &self, - outgoing_kind: &OutgoingKind, + outgoing_kind: &OutgoingDestination, new_events: Vec<(SendingEventType, Vec)>, // Events we want to send: event and full key - current_transaction_status: &mut HashMap, + current_transaction_status: &mut HashMap, ) -> Result>> { let (allow, retry) = self.select_events_current(outgoing_kind.clone(), current_transaction_status)?; @@ -395,7 +395,7 @@ impl Service { } // Add EDU's into the transaction - if let OutgoingKind::Normal(server_name) = outgoing_kind { + if let OutgoingDestination::Normal(server_name) = outgoing_kind { if let Ok((select_edus, last_count)) = self.select_edus(server_name) { events.extend(select_edus.into_iter().map(SendingEventType::Edu)); self.db.set_latest_educount(server_name, last_count)?; @@ -407,7 +407,8 @@ impl Service { #[tracing::instrument(skip(self, outgoing_kind, current_transaction_status))] fn select_events_current( - &self, outgoing_kind: OutgoingKind, current_transaction_status: &mut HashMap, + &self, outgoing_kind: OutgoingDestination, + current_transaction_status: &mut HashMap, ) -> Result<(bool, bool)> { let (mut allow, mut retry) = (true, false); current_transaction_status @@ -597,19 +598,21 @@ pub fn select_edus_receipts( } async fn handle_events( - kind: OutgoingKind, events: Vec, -) -> Result { + kind: OutgoingDestination, events: Vec, +) -> Result { match kind { - OutgoingKind::Appservice(ref id) => handle_events_kind_appservice(&kind, id, events).await, - OutgoingKind::Push(ref userid, ref pushkey) => handle_events_kind_push(&kind, userid, pushkey, events).await, - OutgoingKind::Normal(ref server) => handle_events_kind_normal(&kind, server, events).await, + OutgoingDestination::Appservice(ref id) => handle_events_kind_appservice(&kind, id, events).await, + OutgoingDestination::Push(ref userid, ref pushkey) => { + handle_events_kind_push(&kind, userid, pushkey, events).await + }, + OutgoingDestination::Normal(ref server) => handle_events_kind_normal(&kind, server, events).await, } } #[tracing::instrument(skip(kind, events))] async fn handle_events_kind_appservice( - kind: &OutgoingKind, id: &String, events: Vec, -) -> Result { + kind: &OutgoingDestination, id: &String, events: Vec, +) -> Result { let mut pdu_jsons = Vec::new(); for event in &events { @@ -677,8 +680,8 @@ async fn handle_events_kind_appservice( #[tracing::instrument(skip(kind, events))] async fn handle_events_kind_push( - kind: &OutgoingKind, userid: &OwnedUserId, pushkey: &String, events: Vec, -) -> Result { + kind: &OutgoingDestination, userid: &OwnedUserId, pushkey: &String, events: Vec, +) -> Result { let mut pdus = Vec::new(); for event in &events { @@ -755,8 +758,8 @@ async fn handle_events_kind_push( #[tracing::instrument(skip(kind, events), name = "")] async fn handle_events_kind_normal( - kind: &OutgoingKind, dest: &OwnedServerName, events: Vec, -) -> Result { + kind: &OutgoingDestination, dest: &OwnedServerName, events: Vec, +) -> Result { let mut edu_jsons = Vec::new(); let mut pdu_jsons = Vec::new(); @@ -829,23 +832,23 @@ async fn handle_events_kind_normal( response } -impl OutgoingKind { +impl OutgoingDestination { #[tracing::instrument(skip(self))] pub fn get_prefix(&self) -> Vec { let mut prefix = match self { - OutgoingKind::Appservice(server) => { + OutgoingDestination::Appservice(server) => { let mut p = b"+".to_vec(); p.extend_from_slice(server.as_bytes()); p }, - OutgoingKind::Push(user, pushkey) => { + OutgoingDestination::Push(user, pushkey) => { let mut p = b"$".to_vec(); p.extend_from_slice(user.as_bytes()); p.push(0xFF); p.extend_from_slice(pushkey.as_bytes()); p }, - OutgoingKind::Normal(server) => { + OutgoingDestination::Normal(server) => { let mut p = Vec::new(); p.extend_from_slice(server.as_bytes()); p