Skip to content

Commit

Permalink
Truncate cache if max_messages is set to a lower value at runtime (#…
Browse files Browse the repository at this point in the history
…2957)

Fixes #2884
  • Loading branch information
ivinjabraham authored Aug 30, 2024
1 parent c3d4a33 commit 1db2076
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,18 @@ impl Cache {
///
/// By default, no messages will be cached.
pub fn set_max_messages(&self, max: usize) {
// Check to see if cache has to be truncated
if max < self.settings.read().max_messages {
for mut entry in self.messages.iter_mut() {
let message_queue = entry.value_mut();
let queue_len = message_queue.len();

if queue_len > max {
message_queue.drain(..queue_len - max);
}
}
}

self.settings.write().max_messages = max;
}

Expand Down

0 comments on commit 1db2076

Please sign in to comment.