Refactor sliding-sync.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-09-04 22:33:42 +00:00
parent f6d2ce2f22
commit be1264965a
3 changed files with 1070 additions and 785 deletions

View File

@@ -436,6 +436,7 @@ version = "2.7"
version = "0.3" version = "0.3"
features = [ features = [
"ffi", "ffi",
"serde",
"std", "std",
"union", "union",
] ]

File diff suppressed because it is too large Load Diff

View File

@@ -5,8 +5,11 @@ use std::{
sync::{Arc, Mutex, Mutex as StdMutex}, sync::{Arc, Mutex, Mutex as StdMutex},
}; };
use ruma::{OwnedDeviceId, OwnedRoomId, OwnedUserId, api::client::sync::sync_events::v5}; use ruma::{
use tuwunel_core::Result; OwnedDeviceId, OwnedRoomId, OwnedUserId,
api::client::sync::sync_events::v5::{Request, request},
};
use tuwunel_core::{Result, implement, smallstr::SmallString};
use tuwunel_database::Map; use tuwunel_database::Map;
pub struct Service { pub struct Service {
@@ -31,17 +34,21 @@ pub struct Data {
userid_lastonetimekeyupdate: Arc<Map>, userid_lastonetimekeyupdate: Arc<Map>,
} }
#[derive(Default)] #[derive(Debug, Default)]
struct SnakeSyncCache { struct SnakeSyncCache {
lists: BTreeMap<String, v5::request::List>, lists: BTreeMap<ListId, request::List>,
subscriptions: BTreeMap<OwnedRoomId, v5::request::RoomSubscription>, subscriptions: RoomSubscriptions,
known_rooms: BTreeMap<String, BTreeMap<OwnedRoomId, u64>>, known_rooms: KnownRooms,
extensions: v5::request::Extensions, extensions: request::Extensions,
} }
type DbConnections<K, V> = Mutex<BTreeMap<K, V>>; pub type KnownRooms = BTreeMap<ListId, BTreeMap<OwnedRoomId, u64>>;
type SnakeConnectionsKey = (OwnedUserId, OwnedDeviceId, Option<String>); pub type RoomSubscriptions = BTreeMap<OwnedRoomId, request::RoomSubscription>;
pub type SnakeConnectionsKey = (OwnedUserId, OwnedDeviceId, Option<ConnId>);
type SnakeConnectionsVal = Arc<Mutex<SnakeSyncCache>>; type SnakeConnectionsVal = Arc<Mutex<SnakeSyncCache>>;
type DbConnections<K, V> = Mutex<BTreeMap<K, V>>;
pub type ListId = SmallString<[u8; 16]>;
pub type ConnId = SmallString<[u8; 16]>;
impl crate::Service for Service { impl crate::Service for Service {
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> { fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
@@ -69,38 +76,25 @@ impl crate::Service for Service {
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) } fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
} }
impl Service { #[implement(Service)]
pub fn snake_connection_cached(&self, key: &SnakeConnectionsKey) -> bool {
self.snake_connections
.lock()
.expect("locked")
.contains_key(key)
}
pub fn forget_snake_sync_connection(&self, key: &SnakeConnectionsKey) {
self.snake_connections
.lock()
.expect("locked")
.remove(key);
}
pub fn update_snake_sync_request_with_cache( pub fn update_snake_sync_request_with_cache(
&self, &self,
snake_key: &SnakeConnectionsKey, snake_key: &SnakeConnectionsKey,
request: &mut v5::Request, request: &mut Request,
) -> BTreeMap<String, BTreeMap<OwnedRoomId, u64>> { ) -> KnownRooms {
let mut cache = self.snake_connections.lock().expect("locked"); let mut cache = self.snake_connections.lock().expect("locked");
let cached = Arc::clone( let cached = Arc::clone(
cache cache
.entry(snake_key.clone()) .entry(snake_key.clone())
.or_insert_with(|| Arc::new(Mutex::new(SnakeSyncCache::default()))), .or_insert_with(|| Arc::new(Mutex::new(SnakeSyncCache::default()))),
); );
let cached = &mut cached.lock().expect("locked"); let cached = &mut cached.lock().expect("locked");
drop(cache); drop(cache);
//v5::Request::try_from_http_request(req, path_args); //Request::try_from_http_request(req, path_args);
for (list_id, list) in &mut request.lists { for (list_id, list) in &mut request.lists {
if let Some(cached_list) = cached.lists.get(list_id) { if let Some(cached_list) = cached.lists.get(list_id.as_str()) {
list_or_sticky( list_or_sticky(
&mut list.room_details.required_state, &mut list.room_details.required_state,
&cached_list.room_details.required_state, &cached_list.room_details.required_state,
@@ -113,22 +107,23 @@ impl Service {
some_or_sticky(&mut filters.is_invite, cached_filters.is_invite); some_or_sticky(&mut filters.is_invite, cached_filters.is_invite);
// TODO (morguldir): Find out how a client can unset this, probably need // TODO (morguldir): Find out how a client can unset this, probably need
// to change into an option inside ruma // to change into an option inside ruma
list_or_sticky( list_or_sticky(&mut filters.not_room_types, &cached_filters.not_room_types);
&mut filters.not_room_types,
&cached_filters.not_room_types,
);
}, },
| (_, Some(cached_filters)) => list.filters = Some(cached_filters), | (_, Some(cached_filters)) => list.filters = Some(cached_filters),
| (Some(list_filters), _) => list.filters = Some(list_filters.clone()), | (Some(list_filters), _) => list.filters = Some(list_filters.clone()),
| (..) => {}, | (..) => {},
} }
} }
cached.lists.insert(list_id.clone(), list.clone());
cached
.lists
.insert(list_id.as_str().into(), list.clone());
} }
cached cached
.subscriptions .subscriptions
.extend(request.room_subscriptions.clone()); .extend(request.room_subscriptions.clone());
request request
.room_subscriptions .room_subscriptions
.extend(cached.subscriptions.clone()); .extend(cached.subscriptions.clone());
@@ -163,46 +158,40 @@ impl Service {
.clone() .clone()
.or_else(|| cached.extensions.account_data.rooms.clone()); .or_else(|| cached.extensions.account_data.rooms.clone());
some_or_sticky(&mut request.extensions.typing.enabled, cached.extensions.typing.enabled); {
some_or_sticky( let (request, cached) = (&mut request.extensions.typing, &cached.extensions.typing);
&mut request.extensions.typing.rooms, some_or_sticky(&mut request.enabled, cached.enabled);
cached.extensions.typing.rooms.clone(), some_or_sticky(&mut request.rooms, cached.rooms.clone());
); some_or_sticky(&mut request.lists, cached.lists.clone());
some_or_sticky( };
&mut request.extensions.typing.lists, {
cached.extensions.typing.lists.clone(), let (request, cached) = (&mut request.extensions.receipts, &cached.extensions.receipts);
); some_or_sticky(&mut request.enabled, cached.enabled);
some_or_sticky( some_or_sticky(&mut request.rooms, cached.rooms.clone());
&mut request.extensions.receipts.enabled, some_or_sticky(&mut request.lists, cached.lists.clone());
cached.extensions.receipts.enabled, };
);
some_or_sticky(
&mut request.extensions.receipts.rooms,
cached.extensions.receipts.rooms.clone(),
);
some_or_sticky(
&mut request.extensions.receipts.lists,
cached.extensions.receipts.lists.clone(),
);
cached.extensions = request.extensions.clone(); cached.extensions = request.extensions.clone();
cached.known_rooms.clone() cached.known_rooms.clone()
} }
#[implement(Service)]
pub fn update_snake_sync_known_rooms( pub fn update_snake_sync_known_rooms(
&self, &self,
key: &SnakeConnectionsKey, key: &SnakeConnectionsKey,
list_id: String, list_id: ListId,
new_cached_rooms: BTreeSet<OwnedRoomId>, new_cached_rooms: BTreeSet<OwnedRoomId>,
globalsince: u64, globalsince: u64,
) { ) {
assert!(key.2.is_some(), "Some(conn_id) required for this call"); assert!(key.2.is_some(), "Some(conn_id) required for this call");
let mut cache = self.snake_connections.lock().expect("locked"); let mut cache = self.snake_connections.lock().expect("locked");
let cached = Arc::clone( let cached = Arc::clone(
cache cache
.entry(key.clone()) .entry(key.clone())
.or_insert_with(|| Arc::new(Mutex::new(SnakeSyncCache::default()))), .or_insert_with(|| Arc::new(Mutex::new(SnakeSyncCache::default()))),
); );
let cached = &mut cached.lock().expect("locked"); let cached = &mut cached.lock().expect("locked");
drop(cache); drop(cache);
@@ -216,16 +205,18 @@ impl Service {
*lastsince = 0; *lastsince = 0;
} }
} }
let list = cached.known_rooms.entry(list_id).or_default(); let list = cached.known_rooms.entry(list_id).or_default();
for room_id in new_cached_rooms { for room_id in new_cached_rooms {
list.insert(room_id, globalsince); list.insert(room_id, globalsince);
} }
} }
#[implement(Service)]
pub fn update_snake_sync_subscriptions( pub fn update_snake_sync_subscriptions(
&self, &self,
key: &SnakeConnectionsKey, key: &SnakeConnectionsKey,
subscriptions: BTreeMap<OwnedRoomId, v5::request::RoomSubscription>, subscriptions: RoomSubscriptions,
) { ) {
let mut cache = self.snake_connections.lock().expect("locked"); let mut cache = self.snake_connections.lock().expect("locked");
let cached = Arc::clone( let cached = Arc::clone(
@@ -233,21 +224,41 @@ impl Service {
.entry(key.clone()) .entry(key.clone())
.or_insert_with(|| Arc::new(Mutex::new(SnakeSyncCache::default()))), .or_insert_with(|| Arc::new(Mutex::new(SnakeSyncCache::default()))),
); );
let cached = &mut cached.lock().expect("locked"); let cached = &mut cached.lock().expect("locked");
drop(cache); drop(cache);
cached.subscriptions = subscriptions; cached.subscriptions = subscriptions;
} }
#[implement(Service)]
pub fn forget_snake_sync_connection(&self, key: &SnakeConnectionsKey) {
self.snake_connections
.lock()
.expect("locked")
.remove(key);
}
#[implement(Service)]
pub fn snake_connection_cached(&self, key: &SnakeConnectionsKey) -> bool {
self.snake_connections
.lock()
.expect("locked")
.contains_key(key)
} }
#[inline] #[inline]
pub fn into_snake_key<U, D, C>(user_id: U, device_id: D, conn_id: C) -> SnakeConnectionsKey pub fn into_snake_key<U, D, C>(
user_id: U,
device_id: D,
conn_id: Option<C>,
) -> SnakeConnectionsKey
where where
U: Into<OwnedUserId>, U: Into<OwnedUserId>,
D: Into<OwnedDeviceId>, D: Into<OwnedDeviceId>,
C: Into<Option<String>>, C: Into<ConnId>,
{ {
(user_id.into(), device_id.into(), conn_id.into()) (user_id.into(), device_id.into(), conn_id.map(Into::into))
} }
/// load params from cache if body doesn't contain it, as long as it's allowed /// load params from cache if body doesn't contain it, as long as it's allowed