Skip to content

Commit

Permalink
chore: shrink cache queues (#14105)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jan 30, 2025
1 parent 89f8667 commit 9bb39f1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
18 changes: 16 additions & 2 deletions crates/rpc/rpc-eth-types/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{
future::Future,
pin::Pin,
sync::Arc,
task::{ready, Context, Poll},
task::{Context, Poll},
};
use tokio::sync::{
mpsc::{unbounded_channel, UnboundedSender},
Expand Down Expand Up @@ -324,6 +324,14 @@ where
}
}

/// Shrinks the queues but leaves some space for the next requests
fn shrink_queues(&mut self) {
let min_capacity = 2;
self.full_block_cache.shrink_to(min_capacity);
self.receipts_cache.shrink_to(min_capacity);
self.headers_cache.shrink_to(min_capacity);
}

fn update_cached_metrics(&self) {
self.full_block_cache.update_cached_metrics();
self.receipts_cache.update_cached_metrics();
Expand All @@ -342,7 +350,13 @@ where
let this = self.get_mut();

loop {
match ready!(this.action_rx.poll_next_unpin(cx)) {
let Poll::Ready(action) = this.action_rx.poll_next_unpin(cx) else {
// shrink queues if we don't have any work to do
this.shrink_queues();
return Poll::Pending;
};

match action {
None => {
unreachable!("can't close")
}
Expand Down
6 changes: 6 additions & 0 deletions crates/rpc/rpc-eth-types/src/cache/multi_consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ where
}
}

/// Shrinks the capacity of the queue with a lower limit.
#[inline]
pub fn shrink_to(&mut self, min_capacity: usize) {
self.queued.shrink_to(min_capacity);
}

/// Update metrics for the inner cache.
#[inline]
pub fn update_cached_metrics(&self) {
Expand Down

0 comments on commit 9bb39f1

Please sign in to comment.