Broadcast send_join concurrent to other operations; pre-gather state concurrently.

Broadcast send_knock concurrently.

Concurrent access check for fed event.

Concurrent gather for state responses.

Populate room_version for format_pdu to elide repeated queries.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-09-25 01:05:12 +00:00
parent b9c790326a
commit 85b3de055d
8 changed files with 159 additions and 95 deletions

View File

@@ -1,9 +1,18 @@
use std::{borrow::Borrow, iter::once};
use axum::extract::State;
use futures::{FutureExt, StreamExt, TryStreamExt};
use futures::{
FutureExt, StreamExt, TryFutureExt, TryStreamExt,
future::{join, try_join},
};
use ruma::{OwnedEventId, api::federation::event::get_room_state};
use tuwunel_core::{Result, at, err, utils::IterStream};
use tuwunel_core::{
Result, at, err,
utils::{
future::TryExtExt,
stream::{IterStream, TryBroadbandExt},
},
};
use super::AccessCheck;
use crate::Ruma;
@@ -30,38 +39,50 @@ pub(crate) async fn get_room_state_route(
.await
.map_err(|_| err!(Request(NotFound("PDU state not found."))))?;
let state_ids: Vec<OwnedEventId> = services
let state_ids = services
.state_accessor
.state_full_ids(shortstatehash)
.map(at!(1))
.collect()
.await;
.collect::<Vec<OwnedEventId>>();
let pdus = state_ids
.iter()
.try_stream()
.and_then(|id| services.timeline.get_pdu_json(id))
.and_then(|pdu| {
services
.federation
.format_pdu_into(pdu, None)
.map(Ok)
})
.try_collect()
.await?;
let room_version = services
.state
.get_room_version(&body.room_id)
.ok();
let (room_version, state_ids) = join(room_version, state_ids).await;
let into_federation_format = |pdu| {
services
.federation
.format_pdu_into(pdu, room_version.as_ref())
.map(Ok)
};
let auth_chain = services
.auth_chain
.event_ids_iter(&body.room_id, once(body.event_id.borrow()))
.and_then(async |id| services.timeline.get_pdu_json(&id).await)
.and_then(|pdu| {
.broad_and_then(async |id| {
services
.federation
.format_pdu_into(pdu, None)
.map(Ok)
.timeline
.get_pdu_json(&id)
.and_then(into_federation_format)
.await
})
.try_collect()
.await?;
.try_collect();
let pdus = state_ids
.iter()
.try_stream()
.broad_and_then(|id| {
services
.timeline
.get_pdu_json(id)
.and_then(into_federation_format)
})
.try_collect();
let (auth_chain, pdus) = try_join(auth_chain, pdus).await?;
Ok(get_room_state::v1::Response { auth_chain, pdus })
}