Skip to content

Commit

Permalink
simplifies quic::ConnectionTable::prune_oldest (#30995)
Browse files Browse the repository at this point in the history
  • Loading branch information
behzadnouri authored Mar 31, 2023
1 parent a2797eb commit 8a96b91
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions streamer/src/nonblocking/quic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,29 +932,19 @@ impl ConnectionTable {

fn prune_oldest(&mut self, max_size: usize) -> usize {
let mut num_pruned = 0;
while self.total_size > max_size {
let mut oldest = std::u64::MAX;
let mut oldest_index = None;
for (index, (_key, connections)) in self.table.iter().enumerate() {
for entry in connections {
let last_update = entry.last_update();
if last_update < oldest {
oldest = last_update;
oldest_index = Some(index);
}
}
}
if let Some(oldest_index) = oldest_index {
if let Some((_, removed)) = self.table.swap_remove_index(oldest_index) {
self.total_size -= removed.len();
num_pruned += removed.len();
let key = |(_, connections): &(_, &Vec<_>)| {
connections.iter().map(ConnectionEntry::last_update).min()
};
while self.total_size.saturating_sub(num_pruned) > max_size {
match self.table.values().enumerate().min_by_key(key) {
None => break,
Some((index, connections)) => {
num_pruned += connections.len();
self.table.swap_remove_index(index);
}
} else {
// No valid entries in the table. Continuing the loop will cause
// infinite looping.
break;
}
}
self.total_size = self.total_size.saturating_sub(num_pruned);
num_pruned
}

Expand Down

0 comments on commit 8a96b91

Please sign in to comment.