Skip to content

Commit 574147c

Browse files
committed
fix(sdk): Clear all sliding sync room subscriptions when session expires.
This patch clears all sliding sync room subscriptions when a session expires. Indeed, we might not want to request all room subscriptions when the session restarts. Imagine if the client has subscribed to 400 rooms and the session expires: once the session restarts, it will ask for 400 room subscriptions, which is a lot and will result in a quite slow response.
1 parent f6b21e6 commit 574147c

File tree

1 file changed

+68
-2
lines changed
  • crates/matrix-sdk/src/sliding_sync

1 file changed

+68
-2
lines changed

crates/matrix-sdk/src/sliding_sync/mod.rs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ impl SlidingSync {
756756
Ok(self.inner.internal_channel_send(SlidingSyncInternalMessage::SyncLoopStop)?)
757757
}
758758

759-
/// Expire the current Sliding Sync session.
759+
/// Expire the current Sliding Sync session on the client-side.
760760
///
761761
/// Expiring a Sliding Sync session means: resetting `pos`. It also resets
762762
/// sticky parameters.
@@ -783,7 +783,14 @@ impl SlidingSync {
783783
}
784784

785785
// Force invalidation of all the sticky parameters.
786-
let _ = self.inner.sticky.write().unwrap().data_mut();
786+
{
787+
let mut sticky = self.inner.sticky.write().unwrap();
788+
let data = sticky.data_mut();
789+
790+
// Clear all room subscriptions: we don't want to resend all room subscriptions
791+
// when the session will restart.
792+
data.room_subscriptions.clear();
793+
}
787794

788795
self.inner.lists.read().await.values().for_each(|list| list.invalidate_sticky_data());
789796
}
@@ -1151,6 +1158,65 @@ mod tests {
11511158
Ok(())
11521159
}
11531160

1161+
#[async_test]
1162+
async fn test_room_subscriptions_are_reset_when_session_expires() -> Result<()> {
1163+
let (_server, sliding_sync) = new_sliding_sync(vec![SlidingSyncList::builder("foo")
1164+
.sync_mode(SlidingSyncMode::new_selective().add_range(0..=10))])
1165+
.await?;
1166+
1167+
let room_id_0 = room_id!("!r0:bar.org");
1168+
let room_id_1 = room_id!("!r1:bar.org");
1169+
let room_id_2 = room_id!("!r2:bar.org");
1170+
1171+
// Subscribe to two rooms.
1172+
sliding_sync.subscribe_to_rooms(&[room_id_0, room_id_1], None);
1173+
1174+
{
1175+
let sticky = sliding_sync.inner.sticky.read().unwrap();
1176+
let room_subscriptions = &sticky.data().room_subscriptions;
1177+
1178+
assert!(room_subscriptions.contains_key(&room_id_0.to_owned()));
1179+
assert!(room_subscriptions.contains_key(&room_id_1.to_owned()));
1180+
assert!(room_subscriptions.contains_key(&room_id_2.to_owned()).not());
1181+
}
1182+
1183+
// Subscribe to one more room.
1184+
sliding_sync.subscribe_to_rooms(&[room_id_2], None);
1185+
1186+
{
1187+
let sticky = sliding_sync.inner.sticky.read().unwrap();
1188+
let room_subscriptions = &sticky.data().room_subscriptions;
1189+
1190+
assert!(room_subscriptions.contains_key(&room_id_0.to_owned()));
1191+
assert!(room_subscriptions.contains_key(&room_id_1.to_owned()));
1192+
assert!(room_subscriptions.contains_key(&room_id_2.to_owned()));
1193+
}
1194+
1195+
// Suddenly, the session expires!
1196+
sliding_sync.expire_session().await;
1197+
1198+
{
1199+
let sticky = sliding_sync.inner.sticky.read().unwrap();
1200+
let room_subscriptions = &sticky.data().room_subscriptions;
1201+
1202+
assert!(room_subscriptions.is_empty());
1203+
}
1204+
1205+
// Subscribe to one room again.
1206+
sliding_sync.subscribe_to_rooms(&[room_id_2], None);
1207+
1208+
{
1209+
let sticky = sliding_sync.inner.sticky.read().unwrap();
1210+
let room_subscriptions = &sticky.data().room_subscriptions;
1211+
1212+
assert!(room_subscriptions.contains_key(&room_id_0.to_owned()).not());
1213+
assert!(room_subscriptions.contains_key(&room_id_1.to_owned()).not());
1214+
assert!(room_subscriptions.contains_key(&room_id_2.to_owned()));
1215+
}
1216+
1217+
Ok(())
1218+
}
1219+
11541220
#[async_test]
11551221
async fn test_to_device_token_properly_cached() -> Result<()> {
11561222
let (_server, sliding_sync) = new_sliding_sync(vec![SlidingSyncList::builder("foo")

0 commit comments

Comments
 (0)