Skip to content

Commit 21960a5

Browse files
committed
chore(event cache): add more logs to the auto-shrinking mechanism
1 parent 0819ab1 commit 21960a5

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use tokio::sync::{
5959
broadcast::{error::RecvError, Receiver},
6060
mpsc, Mutex, RwLock,
6161
};
62-
use tracing::{error, info, info_span, instrument, trace, warn, Instrument as _, Span};
62+
use tracing::{debug, error, info, info_span, instrument, trace, warn, Instrument as _, Span};
6363

6464
use self::paginator::PaginatorError;
6565
use crate::{client::WeakClient, Client};
@@ -345,6 +345,8 @@ impl EventCache {
345345
mut rx: mpsc::Receiver<AutoShrinkChannelPayload>,
346346
) {
347347
while let Some(room_id) = rx.recv().await {
348+
trace!(for_room = %room_id, "received notification to shrink");
349+
348350
let room = match inner.for_room(&room_id).await {
349351
Ok(room) => room,
350352
Err(err) => {
@@ -353,6 +355,7 @@ impl EventCache {
353355
}
354356
};
355357

358+
trace!("waiting for state lock…");
356359
let mut state = room.inner.state.write().await;
357360

358361
match state.auto_shrink_if_no_listeners().await {
@@ -364,11 +367,16 @@ impl EventCache {
364367
//
365368
// However, better safe than sorry, and it's cheap to send an update here,
366369
// so let's do it!
367-
let _ =
368-
room.inner.sender.send(RoomEventCacheUpdate::UpdateTimelineEvents {
369-
diffs,
370-
origin: EventsOrigin::Cache,
371-
});
370+
if !diffs.is_empty() {
371+
let _ = room.inner.sender.send(
372+
RoomEventCacheUpdate::UpdateTimelineEvents {
373+
diffs,
374+
origin: EventsOrigin::Cache,
375+
},
376+
);
377+
}
378+
} else {
379+
debug!("auto-shrinking didn't happen");
372380
}
373381
}
374382

crates/matrix-sdk/src/event_cache/room/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ pub struct RoomEventCacheListener {
8585
impl Drop for RoomEventCacheListener {
8686
fn drop(&mut self) {
8787
let previous_listener_count = self.listener_count.fetch_sub(1, Ordering::SeqCst);
88+
89+
trace!("dropping a room event cache listener; previous count: {previous_listener_count}");
90+
8891
if previous_listener_count == 1 {
8992
// We were the last instance of the listener; let the auto-shrinker know by
9093
// notifying it of our room id.
@@ -115,6 +118,8 @@ impl Drop for RoomEventCacheListener {
115118
mpsc::error::TrySendError::Closed(_) => return,
116119
}
117120
}
121+
122+
trace!("sent notification to the parent channel that we were the last listener");
118123
}
119124
}
120125
}
@@ -161,7 +166,8 @@ impl RoomEventCache {
161166
let state = self.inner.state.read().await;
162167
let events = state.events().events().map(|(_position, item)| item.clone()).collect();
163168

164-
state.listener_count.fetch_add(1, Ordering::SeqCst);
169+
let previous_listener_count = state.listener_count.fetch_add(1, Ordering::SeqCst);
170+
trace!("added a room event cache listener; new count: {}", previous_listener_count + 1);
165171

166172
let recv = self.inner.sender.subscribe();
167173
let listener = RoomEventCacheListener {
@@ -956,7 +962,11 @@ mod private {
956962
pub(crate) async fn auto_shrink_if_no_listeners(
957963
&mut self,
958964
) -> Result<Option<Vec<VectorDiff<TimelineEvent>>>, EventCacheError> {
959-
if self.listener_count.load(std::sync::atomic::Ordering::SeqCst) == 0 {
965+
let listener_count = self.listener_count.load(std::sync::atomic::Ordering::SeqCst);
966+
967+
trace!(listener_count, "received request to auto-shrink");
968+
969+
if listener_count == 0 {
960970
// If we are the last strong reference to the auto-shrinker, we can shrink the
961971
// events data structure to its last chunk.
962972
self.shrink_to_last_chunk().await

0 commit comments

Comments
 (0)