fix(service/sending): Use Flush to wake queued destinations

This commit is contained in:
Jade Ellis authored and Ellis Git committed 2026-11-22 17:21:50 +00:00
1 parent d477417909
commit a0d9e71eac
3 files changed
+32 -23

No files matched your search

+15 -9
View File
@@ -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<Destination> {
let mut dests: Vec<Destination> = Vec::new();
let mut last_prefix: Vec<u8> = 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.
+13 -5
View File
@@ -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<usize>) {
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();
}
}
}
+4 -9
View File
@@ -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]