perf: Throttle frequent device metadata updates & centralise site

This commit is contained in:
timedout
2025-12-09 03:25:03 +00:00
parent ba55dffa0e
commit 393d341f07
8 changed files with 76 additions and 106 deletions
+5 -13
View File
@@ -1,4 +1,5 @@
use axum::extract::State; use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use conduwuit::{ use conduwuit::{
Err, Result, at, Err, Result, at,
matrix::{ matrix::{
@@ -22,7 +23,7 @@ use conduwuit_service::{
}; };
use futures::{FutureExt, StreamExt, TryFutureExt, future::OptionFuture, pin_mut}; use futures::{FutureExt, StreamExt, TryFutureExt, future::OptionFuture, pin_mut};
use ruma::{ use ruma::{
DeviceId, MilliSecondsSinceUnixEpoch, RoomId, UserId, DeviceId, RoomId, UserId,
api::{ api::{
Direction, Direction,
client::{filter::RoomEventFilter, message::get_message_events}, client::{filter::RoomEventFilter, message::get_message_events},
@@ -70,6 +71,7 @@ const LIMIT_DEFAULT: usize = 10;
/// where the user was joined, depending on `history_visibility`) /// where the user was joined, depending on `history_visibility`)
pub(crate) async fn get_message_events_route( pub(crate) async fn get_message_events_route(
State(services): State<crate::State>, State(services): State<crate::State>,
InsecureClientIp(client_ip): InsecureClientIp,
body: Ruma<get_message_events::v3::Request>, body: Ruma<get_message_events::v3::Request>,
) -> Result<get_message_events::v3::Response> { ) -> Result<get_message_events::v3::Response> {
debug_assert!(IGNORED_MESSAGE_TYPES.is_sorted(), "IGNORED_MESSAGE_TYPES is not sorted"); debug_assert!(IGNORED_MESSAGE_TYPES.is_sorted(), "IGNORED_MESSAGE_TYPES is not sorted");
@@ -78,20 +80,10 @@ pub(crate) async fn get_message_events_route(
let room_id = &body.room_id; let room_id = &body.room_id;
let filter = &body.filter; let filter = &body.filter;
if sender_device.is_some() {
// Increment the "device last active" metadata
let device_id = body.sender_device();
let mut device = services
.users
.get_device_metadata(sender_user, device_id)
.await
.expect("Device metadata should exist for authenticated device");
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services services
.users .users
.update_device_last_seen(sender_user, device_id, &device) .update_device_last_seen(sender_user, sender_device, client_ip)
.await?; .await;
}
if !services.rooms.metadata.exists(room_id).await { if !services.rooms.metadata.exists(room_id).await {
return Err!(Request(Forbidden("Room does not exist to this server"))); return Err!(Request(Forbidden("Room does not exist to this server")));
+4 -12
View File
@@ -1,6 +1,7 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use axum::extract::State; use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use conduwuit::{Err, PduCount, Result, err}; use conduwuit::{Err, PduCount, Result, err};
use ruma::{ use ruma::{
MilliSecondsSinceUnixEpoch, MilliSecondsSinceUnixEpoch,
@@ -118,23 +119,14 @@ pub(crate) async fn set_read_marker_route(
/// Sets private read marker and public read receipt EDU. /// Sets private read marker and public read receipt EDU.
pub(crate) async fn create_receipt_route( pub(crate) async fn create_receipt_route(
State(services): State<crate::State>, State(services): State<crate::State>,
InsecureClientIp(client_ip): InsecureClientIp,
body: Ruma<create_receipt::v3::Request>, body: Ruma<create_receipt::v3::Request>,
) -> Result<create_receipt::v3::Response> { ) -> Result<create_receipt::v3::Response> {
let sender_user = body.sender_user(); let sender_user = body.sender_user();
if body.sender_device.is_some() {
// Increment the "device last active" metadata
let device_id = body.sender_device();
let mut device = services
.users
.get_device_metadata(sender_user, device_id)
.await
.expect("Device metadata should exist for authenticated device");
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services services
.users .users
.update_device_last_seen(sender_user, device_id, &device) .update_device_last_seen(sender_user, body.sender_device.as_deref(), client_ip)
.await?; .await;
}
if matches!( if matches!(
&body.receipt_type, &body.receipt_type,
+5 -14
View File
@@ -1,8 +1,8 @@
use axum::extract::State; use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use conduwuit::{Err, Result, matrix::pdu::PduBuilder}; use conduwuit::{Err, Result, matrix::pdu::PduBuilder};
use ruma::{ use ruma::{
MilliSecondsSinceUnixEpoch, api::client::redact::redact_event, api::client::redact::redact_event, events::room::redaction::RoomRedactionEventContent,
events::room::redaction::RoomRedactionEventContent,
}; };
use crate::Ruma; use crate::Ruma;
@@ -14,23 +14,14 @@ use crate::Ruma;
/// - TODO: Handle txn id /// - TODO: Handle txn id
pub(crate) async fn redact_event_route( pub(crate) async fn redact_event_route(
State(services): State<crate::State>, State(services): State<crate::State>,
InsecureClientIp(client_ip): InsecureClientIp,
body: Ruma<redact_event::v3::Request>, body: Ruma<redact_event::v3::Request>,
) -> Result<redact_event::v3::Response> { ) -> Result<redact_event::v3::Response> {
let sender_user = body.sender_user(); let sender_user = body.sender_user();
if body.sender_device.is_some() {
// Increment the "device last active" metadata
let device_id = body.sender_device();
let mut device = services
.users
.get_device_metadata(sender_user, device_id)
.await
.expect("Device metadata should exist for authenticated device");
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services services
.users .users
.update_device_last_seen(sender_user, device_id, &device) .update_device_last_seen(sender_user, body.sender_device.as_deref(), client_ip)
.await?; .await;
}
let body = &body.body; let body = &body.body;
if services.users.is_suspended(sender_user).await? { if services.users.is_suspended(sender_user).await? {
// TODO: Users can redact their own messages while suspended // TODO: Users can redact their own messages while suspended
+5 -16
View File
@@ -1,11 +1,9 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use axum::extract::State; use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use conduwuit::{Err, Result, err, matrix::pdu::PduBuilder, utils}; use conduwuit::{Err, Result, err, matrix::pdu::PduBuilder, utils};
use ruma::{ use ruma::{api::client::message::send_message_event, events::MessageLikeEventType};
MilliSecondsSinceUnixEpoch, api::client::message::send_message_event,
events::MessageLikeEventType,
};
use serde_json::from_str; use serde_json::from_str;
use crate::Ruma; use crate::Ruma;
@@ -21,6 +19,7 @@ use crate::Ruma;
/// allowed /// allowed
pub(crate) async fn send_message_event_route( pub(crate) async fn send_message_event_route(
State(services): State<crate::State>, State(services): State<crate::State>,
InsecureClientIp(client_ip): InsecureClientIp,
body: Ruma<send_message_event::v3::Request>, body: Ruma<send_message_event::v3::Request>,
) -> Result<send_message_event::v3::Response> { ) -> Result<send_message_event::v3::Response> {
let sender_user = body.sender_user(); let sender_user = body.sender_user();
@@ -30,20 +29,10 @@ pub(crate) async fn send_message_event_route(
return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); return Err!(Request(UserSuspended("You cannot perform this action while suspended.")));
} }
if sender_device.is_some() {
// Increment the "device last active" metadata
let device_id = sender_device.unwrap();
let mut device = services
.users
.get_device_metadata(sender_user, device_id)
.await
.expect("Device metadata should exist for authenticated device");
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services services
.users .users
.update_device_last_seen(sender_user, device_id, &device) .update_device_last_seen(sender_user, body.sender_device.as_deref(), client_ip)
.await?; .await;
}
// Forbid m.room.encrypted if encryption is disabled // Forbid m.room.encrypted if encryption is disabled
if MessageLikeEventType::RoomEncrypted == body.event_type && !services.config.allow_encryption if MessageLikeEventType::RoomEncrypted == body.event_type && !services.config.allow_encryption
+6 -13
View File
@@ -1,4 +1,5 @@
use axum::extract::State; use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use conduwuit::{ use conduwuit::{
Err, Result, err, Err, Result, err,
matrix::{Event, pdu::PduBuilder}, matrix::{Event, pdu::PduBuilder},
@@ -30,23 +31,14 @@ use crate::{Ruma, RumaResponse};
/// Sends a state event into the room. /// Sends a state event into the room.
pub(crate) async fn send_state_event_for_key_route( pub(crate) async fn send_state_event_for_key_route(
State(services): State<crate::State>, State(services): State<crate::State>,
InsecureClientIp(ip): InsecureClientIp,
body: Ruma<send_state_event::v3::Request>, body: Ruma<send_state_event::v3::Request>,
) -> Result<send_state_event::v3::Response> { ) -> Result<send_state_event::v3::Response> {
let sender_user = body.sender_user(); let sender_user = body.sender_user();
if body.sender_device.is_some() {
// Increment the "device last active" metadata
let device_id = body.sender_device();
let mut device = services
.users
.get_device_metadata(sender_user, device_id)
.await
.expect("Device metadata should exist for authenticated device");
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services services
.users .users
.update_device_last_seen(sender_user, device_id, &device) .update_device_last_seen(sender_user, body.sender_device.as_deref(), ip)
.await?; .await;
}
if services.users.is_suspended(sender_user).await? { if services.users.is_suspended(sender_user).await? {
return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); return Err!(Request(UserSuspended("You cannot perform this action while suspended.")));
@@ -75,9 +67,10 @@ pub(crate) async fn send_state_event_for_key_route(
/// Sends a state event into the room. /// Sends a state event into the room.
pub(crate) async fn send_state_event_for_empty_key_route( pub(crate) async fn send_state_event_for_empty_key_route(
State(services): State<crate::State>, State(services): State<crate::State>,
InsecureClientIp(ip): InsecureClientIp,
body: Ruma<send_state_event::v3::Request>, body: Ruma<send_state_event::v3::Request>,
) -> Result<RumaResponse<send_state_event::v3::Response>> { ) -> Result<RumaResponse<send_state_event::v3::Response>> {
send_state_event_for_key_route(State(services), body) send_state_event_for_key_route(State(services), InsecureClientIp(ip), body)
.boxed() .boxed()
.await .await
.map(RumaResponse) .map(RumaResponse)
+5 -10
View File
@@ -6,6 +6,7 @@ use std::{
}; };
use axum::extract::State; use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use conduwuit::{ use conduwuit::{
Err, Error, Result, at, error, extract_variant, is_equal_to, Err, Error, Result, at, error, extract_variant, is_equal_to,
matrix::{Event, TypeStateKey, pdu::PduCount}, matrix::{Event, TypeStateKey, pdu::PduCount},
@@ -25,7 +26,7 @@ use futures::{
pin_mut, pin_mut,
}; };
use ruma::{ use ruma::{
DeviceId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, RoomId, UInt, UserId, DeviceId, OwnedEventId, OwnedRoomId, RoomId, UInt, UserId,
api::client::sync::sync_events::{self, DeviceLists, UnreadNotificationsCount}, api::client::sync::sync_events::{self, DeviceLists, UnreadNotificationsCount},
directory::RoomTypeFilter, directory::RoomTypeFilter,
events::{ events::{
@@ -61,23 +62,17 @@ type KnownRooms = BTreeMap<String, BTreeMap<OwnedRoomId, u64>>;
/// [MSC4186]: https://github.com/matrix-org/matrix-spec-proposals/pull/4186 /// [MSC4186]: https://github.com/matrix-org/matrix-spec-proposals/pull/4186
pub(crate) async fn sync_events_v5_route( pub(crate) async fn sync_events_v5_route(
State(ref services): State<crate::State>, State(ref services): State<crate::State>,
InsecureClientIp(client_ip): InsecureClientIp,
body: Ruma<sync_events::v5::Request>, body: Ruma<sync_events::v5::Request>,
) -> Result<sync_events::v5::Response> { ) -> Result<sync_events::v5::Response> {
debug_assert!(DEFAULT_BUMP_TYPES.is_sorted(), "DEFAULT_BUMP_TYPES is not sorted"); debug_assert!(DEFAULT_BUMP_TYPES.is_sorted(), "DEFAULT_BUMP_TYPES is not sorted");
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sender_device = body.sender_device.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated");
// Increment the "device last active" metadata
let mut device = services
.users
.get_device_metadata(sender_user, sender_device)
.await
.expect("Device metadata should exist for authenticated device");
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services services
.users .users
.update_device_last_seen(sender_user, sender_device, &device) .update_device_last_seen(sender_user, Some(sender_device), client_ip)
.await?; .await;
let mut body = body.body; let mut body = body.body;
+5 -13
View File
@@ -1,6 +1,7 @@
use axum::extract::State; use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use conduwuit::{Err, Result, utils, utils::math::Tried}; use conduwuit::{Err, Result, utils, utils::math::Tried};
use ruma::{MilliSecondsSinceUnixEpoch, api::client::typing::create_typing_event}; use ruma::api::client::typing::create_typing_event;
use crate::Ruma; use crate::Ruma;
@@ -9,24 +10,15 @@ use crate::Ruma;
/// Sets the typing state of the sender user. /// Sets the typing state of the sender user.
pub(crate) async fn create_typing_event_route( pub(crate) async fn create_typing_event_route(
State(services): State<crate::State>, State(services): State<crate::State>,
InsecureClientIp(ip): InsecureClientIp,
body: Ruma<create_typing_event::v3::Request>, body: Ruma<create_typing_event::v3::Request>,
) -> Result<create_typing_event::v3::Response> { ) -> Result<create_typing_event::v3::Response> {
use create_typing_event::v3::Typing; use create_typing_event::v3::Typing;
let sender_user = body.sender_user(); let sender_user = body.sender_user();
if body.sender_device.is_some() {
// Increment the "device last active" metadata
let device_id = body.sender_device();
let mut device = services
.users
.get_device_metadata(sender_user, device_id)
.await
.expect("Device metadata should exist for authenticated device");
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services services
.users .users
.update_device_last_seen(sender_user, device_id, &device) .update_device_last_seen(sender_user, body.sender_device.as_deref(), ip)
.await?; .await;
}
if sender_user != body.user_id && body.appservice_info.is_none() { if sender_user != body.user_id && body.appservice_info.is_none() {
return Err!(Request(Forbidden("You cannot update typing status of other users."))); return Err!(Request(Forbidden("You cannot update typing status of other users.")));
+29 -3
View File
@@ -1,6 +1,6 @@
#[cfg(feature = "ldap")] #[cfg(feature = "ldap")]
use std::collections::HashMap; use std::collections::HashMap;
use std::{collections::BTreeMap, mem, sync::Arc}; use std::{collections::BTreeMap, mem, net::IpAddr, sync::Arc};
#[cfg(feature = "ldap")] #[cfg(feature = "ldap")]
use conduwuit::result::LogErr; use conduwuit::result::LogErr;
@@ -25,6 +25,7 @@ use ruma::{
invite_permission_config::{FilterLevel, InvitePermissionConfigEvent}, invite_permission_config::{FilterLevel, InvitePermissionConfigEvent},
}, },
serde::Raw, serde::Raw,
uint,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::json; use serde_json::json;
@@ -988,7 +989,7 @@ impl Service {
device: &Device, device: &Device,
) -> Result<()> { ) -> Result<()> {
increment(&self.db.userid_devicelistversion, user_id.as_bytes()); increment(&self.db.userid_devicelistversion, user_id.as_bytes());
self.update_device_last_seen(user_id, device_id, device) self.update_device_metadata_no_increment(user_id, device_id, device)
.await .await
} }
@@ -996,7 +997,7 @@ impl Service {
// This is namely used for updating the last_seen_ip and last_seen_ts values, // This is namely used for updating the last_seen_ip and last_seen_ts values,
// as those do not need a device list version bump due to them not being // as those do not need a device list version bump due to them not being
// relevant to other consumers. // relevant to other consumers.
pub async fn update_device_last_seen( pub async fn update_device_metadata_no_increment(
&self, &self,
user_id: &UserId, user_id: &UserId,
device_id: &DeviceId, device_id: &DeviceId,
@@ -1008,6 +1009,31 @@ impl Service {
Ok(()) Ok(())
} }
pub async fn update_device_last_seen(
&self,
user_id: &UserId,
device_id: Option<&DeviceId>,
ip: IpAddr,
) {
let now = MilliSecondsSinceUnixEpoch::now();
if let Some(device_id) = device_id {
if let Ok(mut device) = self.get_device_metadata(user_id, device_id).await {
device.last_seen_ip = Some(ip.to_string());
// If the last update was less than 10 seconds ago, don't update the timestamp
if let Some(prev) = device.last_seen_ts {
if now.get().saturating_sub(prev.get()) < uint!(10_000) {
return;
}
}
device.last_seen_ts = Some(now);
self.update_device_metadata_no_increment(user_id, device_id, &device)
.await
.ok();
}
}
}
/// Get device metadata. /// Get device metadata.
pub async fn get_device_metadata( pub async fn get_device_metadata(
&self, &self,