From a0d9e71eacb296b34392280c927fd6dfd2cc7d89 Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Sat, 21 Nov 2026 21:54:14 +0000 Subject: [PATCH] fix(service/sending): Use Flush to wake queued destinations --- src/service/sending/data.rs | 24 +++++++++++++++--------- src/service/sending/mod.rs | 18 +++++++++++++----- src/service/sending/tests.rs | 13 ++++--------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/service/sending/data.rs b/src/service/sending/data.rs index 3f34bca91..fc8bc03a0 100644 --- a/src/service/sending/data.rs +++ b/src/service/sending/data.rs @@ -170,22 +170,28 @@ impl Data { }) } - /// The oldest queued request of each destination, not yet in flight. - pub(super) async fn queued_heads(&self) -> Vec<(Destination, QueueItem)> { - let mut heads: Vec<(Destination, QueueItem)> = Vec::new(); + /// Destinations with at least one queued request not yet in flight. + pub(super) async fn queued_destinations(&self) -> Vec { + let mut dests: Vec = Vec::new(); + let mut last_prefix: Vec = Vec::new(); self.servernameevent_data .raw_stream() .ignore_err() - .ready_for_each(|(key, val)| match parse_servercurrentevent(key, val) { - | Ok((dest, event)) => - if heads.last().is_none_or(|(last, _)| *last != dest) { - heads.push((dest, (key.to_vec(), event))); + .ready_for_each(|(key, val)| { + if !last_prefix.is_empty() && key.starts_with(&last_prefix) { + return; + } + match parse_servercurrentevent(key, val) { + | Ok((dest, _)) => { + last_prefix = dest.get_prefix(); + dests.push(dest); }, - | Err(e) => warn!(?key, "invalid queued request key: {e}"), + | Err(e) => warn!(?key, "invalid queued request key: {e}"), + } }) .await; - heads + dests } /// Estimated number of requests waiting to be sent and in flight. diff --git a/src/service/sending/mod.rs b/src/service/sending/mod.rs index 180f8da12..8cb9b43b6 100644 --- a/src/service/sending/mod.rs +++ b/src/service/sending/mod.rs @@ -540,14 +540,22 @@ impl Service { sender.send(msg).map_err(|e| err!("{e}")) } - /// Hands the oldest unsent request of each destination to its worker, so - /// a backlog with nothing in flight is sent without waiting for a new - /// event. Bypasses `dispatch` as these requests were already counted. + /// Wakes workers for every destination that has unsent queued requests, so + /// a backlog with nothing in flight is drained without waiting for a new + /// event. Uses `SendingEvent::Flush` so a message racing with already- + /// buffered channel items cannot re-insert and duplicate a sent request. async fn dispatch_queued(&self, shard: Option) { - for (dest, (queue_id, event)) in self.db.queued_heads().await { + for dest in self.db.queued_destinations().await { let id = self.shard_id(&dest); if shard.is_none_or(|shard| shard == id) { - self.channels[id].0.send(Msg { dest, event, queue_id }).ok(); + self.channels[id] + .0 + .send(Msg { + dest, + event: SendingEvent::Flush, + queue_id: Vec::new(), + }) + .ok(); } } } diff --git a/src/service/sending/tests.rs b/src/service/sending/tests.rs index 40c1cb957..be466d65e 100644 --- a/src/service/sending/tests.rs +++ b/src/service/sending/tests.rs @@ -7,29 +7,24 @@ use super::{Destination, EduBuf, SendingEvent, restart_delay}; use crate::test_utils::test_services; #[tokio::test] -async fn queued_heads_lists_the_oldest_unsent_request_per_destination() { +async fn queued_destinations_lists_each_destination_with_unsent_requests() { let (services, path, _database) = test_services().await; let db = &services.sending.db; let queued = Destination::Federation(server_name!("queued.example").to_owned()); let active = Destination::Federation(server_name!("active.example").to_owned()); let event = SendingEvent::Edu(EduBuf::from_slice(br#"{"edu_type":"m.typing"}"#)); - let first = db.queue_requests(once((&event, &queued))); + db.queue_requests(once((&event, &queued))); db.queue_requests(once((&event, &queued))); db.queue_requests(once((&event, &active))); let in_flight: Vec<_> = db.queued_requests(&active).collect().await; db.mark_as_active(in_flight.iter()); - let heads: Vec<_> = db - .queued_heads() - .await - .into_iter() - .map(|(dest, (key, _))| (dest, key)) - .collect(); + let dests = db.queued_destinations().await; drop(services); std::fs::remove_dir_all(path).unwrap(); - assert_eq!(heads, [(queued, first[0].clone())]); + assert_eq!(dests, [queued]); } #[test]