Skip to content

Commit 119bee6

Browse files
committed
feat(sdk,ui): SlidingSync::subscribe_to_rooms has a new cancel_in_flight_request argument.
This patch adds a new `cancel_in_flight_request` argument to `SlidingSync::subscribe_to_rooms`, which tells the method cancel the in-flight request if any. This patch also updates `RoomListService::subscribe_to_rooms` to turn this new argument to `true` if the state machine isn't in a “starting” state. The problem it's solving is the following: * some apps starts the room list service * a first request is sent with `pos = None` * the server calculates a new session (which can be expensive) * the app subscribes to a set of rooms * a second request is immediately sent with `pos = None` again * the server does possibly NOT cancel its previous calculations, but starts a new session and its calculations This is pretty expensive for the server. This patch makes so that the immediate room subscriptions will be part of the second request, with the first request not being cancelled.
1 parent 4fd4410 commit 119bee6

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

crates/matrix-sdk-ui/src/notification_client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ impl NotificationClient {
380380
required_state,
381381
timeline_limit: Some(uint!(16))
382382
})),
383+
true,
383384
);
384385

385386
let mut remaining_attempts = 3;

crates/matrix-sdk-ui/src/room_list_service/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,14 @@ impl RoomListService {
396396
settings.required_state.push((StateEventType::RoomCreate, "".to_owned()));
397397
}
398398

399-
self.sliding_sync.subscribe_to_rooms(room_ids, Some(settings))
399+
let cancel_in_flight_request = match self.state.get() {
400+
State::Init | State::Recovering | State::Error { .. } | State::Terminated { .. } => {
401+
false
402+
}
403+
State::SettingUp | State::Running => true,
404+
};
405+
406+
self.sliding_sync.subscribe_to_rooms(room_ids, Some(settings), cancel_in_flight_request)
400407
}
401408

402409
#[cfg(test)]

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,13 @@ impl SlidingSync {
151151
&self,
152152
room_ids: &[&RoomId],
153153
settings: Option<http::request::RoomSubscription>,
154+
cancel_in_flight_request: bool,
154155
) {
155156
let settings = settings.unwrap_or_default();
156157
let mut sticky = self.inner.sticky.write().unwrap();
157158
let room_subscriptions = &mut sticky.data_mut().room_subscriptions;
158159

159-
let mut skip_sync_loop = false;
160+
let mut skip_over_current_sync_loop_iteration = false;
160161

161162
for room_id in room_ids {
162163
// If the room subscription already exists, let's not
@@ -172,11 +173,11 @@ impl SlidingSync {
172173

173174
entry.insert((RoomSubscriptionState::default(), settings.clone()));
174175

175-
skip_sync_loop = true;
176+
skip_over_current_sync_loop_iteration = true;
176177
}
177178
}
178179

179-
if skip_sync_loop {
180+
if cancel_in_flight_request && skip_over_current_sync_loop_iteration {
180181
self.inner.internal_channel_send_if_possible(
181182
SlidingSyncInternalMessage::SyncLoopSkipOverCurrentIteration,
182183
);
@@ -1219,7 +1220,7 @@ mod tests {
12191220
// Members are now synced! We can start subscribing and see how it goes.
12201221
assert!(room0.are_members_synced());
12211222

1222-
sliding_sync.subscribe_to_rooms(&[room_id_0, room_id_1], None);
1223+
sliding_sync.subscribe_to_rooms(&[room_id_0, room_id_1], None, true);
12231224

12241225
// OK, we have subscribed to some rooms. Let's check on `room0` if members are
12251226
// now marked as not synced.
@@ -1260,7 +1261,7 @@ mod tests {
12601261
// Members are synced, good, good.
12611262
assert!(room0.are_members_synced());
12621263

1263-
sliding_sync.subscribe_to_rooms(&[room_id_0], None);
1264+
sliding_sync.subscribe_to_rooms(&[room_id_0], None, false);
12641265

12651266
// Members are still synced: because we have already subscribed to the
12661267
// room, the members aren't marked as unsynced.
@@ -1280,7 +1281,7 @@ mod tests {
12801281
let room_id_2 = room_id!("!r2:bar.org");
12811282

12821283
// Subscribe to two rooms.
1283-
sliding_sync.subscribe_to_rooms(&[room_id_0, room_id_1], None);
1284+
sliding_sync.subscribe_to_rooms(&[room_id_0, room_id_1], None, false);
12841285

12851286
{
12861287
let sticky = sliding_sync.inner.sticky.read().unwrap();
@@ -1292,7 +1293,7 @@ mod tests {
12921293
}
12931294

12941295
// Subscribe to one more room.
1295-
sliding_sync.subscribe_to_rooms(&[room_id_2], None);
1296+
sliding_sync.subscribe_to_rooms(&[room_id_2], None, false);
12961297

12971298
{
12981299
let sticky = sliding_sync.inner.sticky.read().unwrap();
@@ -1314,7 +1315,7 @@ mod tests {
13141315
}
13151316

13161317
// Subscribe to one room again.
1317-
sliding_sync.subscribe_to_rooms(&[room_id_2], None);
1318+
sliding_sync.subscribe_to_rooms(&[room_id_2], None, false);
13181319

13191320
{
13201321
let sticky = sliding_sync.inner.sticky.read().unwrap();

0 commit comments

Comments
 (0)