sending.rs: add RequestKey

Much easier to reason about than with a bunch of Vec<u8> everywhere.
This commit is contained in:
Lambda 2024-05-22 18:28:26 +00:00
parent 507de063f5
commit e294543ddb
3 changed files with 50 additions and 27 deletions

View file

@ -88,15 +88,28 @@ pub(crate) enum SendingEventType {
Edu(Vec<u8>),
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct RequestKey(Vec<u8>);
impl RequestKey {
pub(crate) fn new(key: Vec<u8>) -> Self {
Self(key)
}
pub(crate) fn as_bytes(&self) -> &[u8] {
&self.0
}
}
pub(crate) struct Service {
db: &'static dyn Data,
/// The state for a given state hash.
pub(super) maximum_requests: Arc<Semaphore>,
pub(crate) sender:
mpsc::UnboundedSender<(OutgoingKind, SendingEventType, Vec<u8>)>,
mpsc::UnboundedSender<(OutgoingKind, SendingEventType, RequestKey)>,
receiver: Mutex<
mpsc::UnboundedReceiver<(OutgoingKind, SendingEventType, Vec<u8>)>,
mpsc::UnboundedReceiver<(OutgoingKind, SendingEventType, RequestKey)>,
>,
}
@ -284,7 +297,7 @@ impl Service {
&self,
outgoing_kind: OutgoingKind,
event: SendingEventType,
key: Vec<u8>,
key: RequestKey,
current_transaction_status: &mut TransactionStatusMap,
) -> Option<HandlerInputs> {
if let Ok(Some(events)) = self.select_events(
@ -312,7 +325,7 @@ impl Service {
&self,
outgoing_kind: &OutgoingKind,
// Events we want to send: event and full key
new_events: Vec<(SendingEventType, Vec<u8>)>,
new_events: Vec<(SendingEventType, RequestKey)>,
current_transaction_status: &mut HashMap<
OutgoingKind,
TransactionStatus,

View file

@ -1,6 +1,6 @@
use ruma::ServerName;
use super::{OutgoingKind, SendingEventType};
use super::{OutgoingKind, RequestKey, SendingEventType};
use crate::Result;
pub(crate) trait Data: Send + Sync {
@ -8,14 +8,15 @@ pub(crate) trait Data: Send + Sync {
fn active_requests<'a>(
&'a self,
) -> Box<
dyn Iterator<Item = Result<(Vec<u8>, OutgoingKind, SendingEventType)>>
+ 'a,
dyn Iterator<
Item = Result<(RequestKey, OutgoingKind, SendingEventType)>,
> + 'a,
>;
fn active_requests_for<'a>(
&'a self,
outgoing_kind: &OutgoingKind,
) -> Box<dyn Iterator<Item = Result<(Vec<u8>, SendingEventType)>> + 'a>;
fn delete_active_request(&self, key: Vec<u8>) -> Result<()>;
) -> Box<dyn Iterator<Item = Result<(RequestKey, SendingEventType)>> + 'a>;
fn delete_active_request(&self, key: RequestKey) -> Result<()>;
fn delete_all_active_requests_for(
&self,
outgoing_kind: &OutgoingKind,
@ -23,14 +24,14 @@ pub(crate) trait Data: Send + Sync {
fn queue_requests(
&self,
requests: &[(&OutgoingKind, SendingEventType)],
) -> Result<Vec<Vec<u8>>>;
) -> Result<Vec<RequestKey>>;
fn queued_requests<'a>(
&'a self,
outgoing_kind: &OutgoingKind,
) -> Box<dyn Iterator<Item = Result<(SendingEventType, Vec<u8>)>> + 'a>;
) -> Box<dyn Iterator<Item = Result<(SendingEventType, RequestKey)>> + 'a>;
fn mark_as_active(
&self,
events: &[(SendingEventType, Vec<u8>)],
events: &[(SendingEventType, RequestKey)],
) -> Result<()>;
fn set_latest_educount(
&self,