significantly improve get_missing_events fed code

Signed-off-by: June Clementine Strawberry <june@3.dog>
This commit is contained in:
June Clementine Strawberry 2025-04-02 22:44:44 -04:00
parent ea246d91d9
commit 74012c5289
No known key found for this signature in database
2 changed files with 65 additions and 58 deletions

View file

@ -6,11 +6,17 @@ use conduwuit::{
utils::{IterStream, ReadyExt, stream::TryTools},
};
use futures::{FutureExt, StreamExt, TryStreamExt};
use ruma::{MilliSecondsSinceUnixEpoch, api::federation::backfill::get_backfill, uint};
use ruma::{MilliSecondsSinceUnixEpoch, api::federation::backfill::get_backfill};
use super::AccessCheck;
use crate::Ruma;
/// arbitrary number but synapse's is 100 and we can handle lots of these
/// anyways
const LIMIT_MAX: usize = 150;
/// no spec defined number but we can handle a lot of these
const LIMIT_DEFAULT: usize = 50;
/// # `GET /_matrix/federation/v1/backfill/<room_id>`
///
/// Retrieves events from before the sender joined the room, if the room's
@ -30,9 +36,9 @@ pub(crate) async fn get_backfill_route(
let limit = body
.limit
.min(uint!(100))
.try_into()
.expect("UInt could not be converted to usize");
.unwrap_or(LIMIT_DEFAULT)
.min(LIMIT_MAX);
let from = body
.v

View file

@ -1,13 +1,19 @@
use axum::extract::State;
use conduwuit::{Error, Result};
use ruma::{
CanonicalJsonValue, EventId, RoomId,
api::{client::error::ErrorKind, federation::event::get_missing_events},
use conduwuit::{
Result, debug, debug_info, debug_warn,
utils::{self},
warn,
};
use ruma::api::federation::event::get_missing_events;
use super::AccessCheck;
use crate::Ruma;
/// arbitrary number but synapse's is 20 and we can handle lots of these anyways
const LIMIT_MAX: usize = 50;
/// spec says default is 10
const LIMIT_DEFAULT: usize = 10;
/// # `POST /_matrix/federation/v1/get_missing_events/{roomId}`
///
/// Retrieves events that the sender is missing.
@ -24,7 +30,11 @@ pub(crate) async fn get_missing_events_route(
.check()
.await?;
let limit = body.limit.try_into()?;
let limit = body
.limit
.try_into()
.unwrap_or(LIMIT_DEFAULT)
.min(LIMIT_MAX);
let mut queued_events = body.latest_events.clone();
// the vec will never have more entries the limit
@ -32,22 +42,19 @@ pub(crate) async fn get_missing_events_route(
let mut i: usize = 0;
while i < queued_events.len() && events.len() < limit {
if let Ok(pdu) = services
.rooms
.timeline
.get_pdu_json(&queued_events[i])
.await
{
let room_id_str = pdu
.get("room_id")
.and_then(|val| val.as_str())
.ok_or_else(|| Error::bad_database("Invalid event in database."))?;
let Ok(pdu) = services.rooms.timeline.get_pdu(&queued_events[i]).await else {
debug_info!(?body.origin, "Event {} does not exist locally, skipping", &queued_events[i]);
i = i.saturating_add(1);
continue;
};
let event_room_id = <&RoomId>::try_from(room_id_str)
.map_err(|_| Error::bad_database("Invalid room_id in event in database."))?;
if event_room_id != body.room_id {
return Err(Error::BadRequest(ErrorKind::InvalidParam, "Event from wrong room."));
if pdu.room_id != body.room_id {
warn!(?body.origin,
"Got an event for the wrong room in database. Found {:?} in {:?}, server requested events in {:?}. Skipping.",
pdu.event_id, pdu.room_id, body.room_id
);
i = i.saturating_add(1);
continue;
}
if body.earliest_events.contains(&queued_events[i]) {
@ -61,32 +68,26 @@ pub(crate) async fn get_missing_events_route(
.server_can_see_event(body.origin(), &body.room_id, &queued_events[i])
.await
{
debug!(?body.origin, "Server cannot see {:?} in {:?}, skipping", pdu.event_id, pdu.room_id);
i = i.saturating_add(1);
continue;
}
let prev_events = pdu
.get("prev_events")
.and_then(CanonicalJsonValue::as_array)
.unwrap_or_default();
let Ok(pdu_json) = utils::to_canonical_object(&pdu) else {
debug_warn!(?body.origin, "Failed to convert PDU in database to canonical JSON: {pdu:?}");
i = i.saturating_add(1);
continue;
};
queued_events.extend(
prev_events
.iter()
.map(<&EventId>::try_from)
.filter_map(Result::ok)
.map(ToOwned::to_owned),
);
queued_events.extend(pdu.prev_events.iter().map(ToOwned::to_owned));
events.push(
services
.sending
.convert_to_outgoing_federation_event(pdu)
.convert_to_outgoing_federation_event(pdu_json)
.await,
);
}
i = i.saturating_add(1);
}
Ok(get_missing_events::v1::Response { events })
}