Skip to content

Commit

Permalink
Use filter fn
Browse files Browse the repository at this point in the history
  • Loading branch information
mxinden committed Jul 10, 2024
1 parent 5c06141 commit 32635f5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
22 changes: 11 additions & 11 deletions neqo-transport/src/cc/classic_cc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,14 @@ impl<T: WindowAdjustment> CongestionControl for ClassicCongestionControl<T> {
&[QlogMetric::BytesInFlight(self.bytes_in_flight)],
);

// Lost PMTUD probes do not elicit a congestion control reaction.
let probe_size = self.pmtud.probe_size();
let probe_sent = self.pmtud.probe_sent();
let is_pmtud_probe = self.pmtud.is_pmtud_probe_filter();
let mut lost_packets = lost_packets
.iter()
.filter(|pkt| !probe_sent || pkt.len() < probe_size)
.filter(|pkt| !is_pmtud_probe(pkt))
.rev()
.peekable();

// Lost PMTUD probes do not elicit a congestion control reaction.
let Some(last_lost_packet) = lost_packets.peek() else {
return false;
};
Expand Down Expand Up @@ -469,16 +469,13 @@ impl<T: WindowAdjustment> ClassicCongestionControl<T> {
}
}

fn detect_persistent_congestion<'a, I>(
fn detect_persistent_congestion<'a>(
&mut self,
first_rtt_sample_time: Option<Instant>,
prev_largest_acked_sent: Option<Instant>,
pto: Duration,
lost_packets: I,
) -> bool
where
I: Iterator<Item = &'a SentPacket>,
{
lost_packets: impl IntoIterator<Item = &'a SentPacket>,
) -> bool {
if first_rtt_sample_time.is_none() {
return false;
}
Expand All @@ -493,7 +490,10 @@ impl<T: WindowAdjustment> ClassicCongestionControl<T> {
// Also, make sure to ignore any packets sent before we got an RTT estimate
// as we might not have sent PTO packets soon enough after those.
let cutoff = max(first_rtt_sample_time, prev_largest_acked_sent);
for p in lost_packets.skip_while(|p| Some(p.time_sent()) < cutoff) {
for p in lost_packets
.into_iter()
.skip_while(|p| Some(p.time_sent()) < cutoff)
{
if p.pn() != last_pn + 1 {
// Not a contiguous range of lost packets, start over.
start = None;
Expand Down
18 changes: 15 additions & 3 deletions neqo-transport/src/pmtud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,26 @@ impl Pmtud {
);
}

/// Provides a [`Fn`] that returns true if the packet is a PMTUD probe.
///
/// Allows filtering packets without holding a reference to [`Pmtud`]. When
/// in doubt, use [`Pmtud::is_pmtud_probe`].
#[must_use]
pub fn is_pmtud_probe_filter(&self) -> impl Fn(&SentPacket) -> bool {
let probe_state = Probe::Sent;
let probe_size = self.probe_size();

move |p: &SentPacket| -> bool { probe_state == Probe::Sent && p.len() == probe_size }
}

/// Returns true if the packet is a PMTUD probe.
fn is_probe(&self, p: &SentPacket) -> bool {
self.probe_state == Probe::Sent && p.len() == self.probe_size()
fn is_pmtud_probe(&self, p: &SentPacket) -> bool {
self.is_pmtud_probe_filter()(p)
}

/// Count the PMTUD probes included in `pkts`.
fn count_probes(&self, pkts: &[SentPacket]) -> usize {
pkts.iter().filter(|p| self.is_probe(p)).count()
pkts.iter().filter(|p| self.is_pmtud_probe(p)).count()
}

/// Checks whether a PMTUD probe has been acknowledged, and if so, updates the PMTUD state.
Expand Down

0 comments on commit 32635f5

Please sign in to comment.