Skip to content

Commit eb64834

Browse files
optimize too-many-tx-drops detection
1 parent d7b9e8c commit eb64834

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

packages/adapters/eth/src/failover/error_tracker.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,17 @@ impl ErrorTracker {
9494
mempool_drop_threshold: usize,
9595
mempool_drop_window: Duration,
9696
) -> bool {
97-
let now = Instant::now();
9897
let failure_window = self.mempool_drop_window.read().await;
98+
let cutoff = Instant::now() - mempool_drop_window;
9999

100-
// Count entries within the time window
101-
let valid_entries_count = failure_window
100+
// Since entries are ordered by timestamp, we can iterate from the back
101+
// and stop as soon as we find an entry outside the time window
102+
failure_window
102103
.iter()
103-
.filter(|&timestamp| now.duration_since(*timestamp) <= mempool_drop_window)
104-
.count();
105-
106-
valid_entries_count >= mempool_drop_threshold
104+
.rev()
105+
.take_while(|&&timestamp| timestamp >= cutoff)
106+
.count()
107+
>= mempool_drop_threshold
107108
}
108109

109110
pub async fn note_mempool_drop(

0 commit comments

Comments
 (0)