use crate::Duration;
use crate::frame::AckFrequency;
use crate::transport_parameters::TransportParameters;
use crate::{AckFrequencyConfig, TIMER_GRANULARITY, TransportError, VarInt};
use super::PathId;
pub(super) struct AckFrequencyState {
in_flight_ack_frequency_frame: Option<(PathId, u64, Duration)>,
next_outgoing_sequence_number: VarInt,
pub(super) peer_max_ack_delay: Duration,
last_ack_frequency_frame: Option<u64>,
pub(super) max_ack_delay: Duration,
}
impl AckFrequencyState {
pub(super) fn new(default_max_ack_delay: Duration) -> Self {
Self {
in_flight_ack_frequency_frame: None,
next_outgoing_sequence_number: VarInt(0),
peer_max_ack_delay: default_max_ack_delay,
last_ack_frequency_frame: None,
max_ack_delay: default_max_ack_delay,
}
}
pub(super) fn candidate_max_ack_delay(
&self,
rtt: Duration,
config: &AckFrequencyConfig,
peer_params: &TransportParameters,
) -> Duration {
let min_ack_delay =
Duration::from_micros(peer_params.min_ack_delay.map_or(0, |x| x.into()));
config
.max_ack_delay
.unwrap_or(self.peer_max_ack_delay)
.clamp(min_ack_delay, rtt.max(MIN_AUTOMATIC_ACK_DELAY))
}
pub(super) fn max_ack_delay_for_pto(&self) -> Duration {
if let Some((_, _, max_ack_delay)) = self.in_flight_ack_frequency_frame {
self.peer_max_ack_delay.max(max_ack_delay)
} else {
self.peer_max_ack_delay
}
}
pub(super) fn next_sequence_number(&mut self) -> VarInt {
assert!(self.next_outgoing_sequence_number <= VarInt::MAX);
let seq = self.next_outgoing_sequence_number;
self.next_outgoing_sequence_number.0 += 1;
seq
}
pub(super) fn should_send_ack_frequency(
&self,
rtt: Duration,
config: &AckFrequencyConfig,
peer_params: &TransportParameters,
) -> bool {
if self.next_outgoing_sequence_number.0 == 0 {
return true;
}
let current = self
.in_flight_ack_frequency_frame
.map_or(self.peer_max_ack_delay, |(_, _, pending)| pending);
let desired = self.candidate_max_ack_delay(rtt, config, peer_params);
let error = (desired.as_secs_f32() / current.as_secs_f32()) - 1.0;
error.abs() > MAX_RTT_ERROR
}
pub(super) fn ack_frequency_sent(
&mut self,
path_id: PathId,
pn: u64,
requested_max_ack_delay: Duration,
) {
self.in_flight_ack_frequency_frame = Some((path_id, pn, requested_max_ack_delay));
}
pub(super) fn on_acked(&mut self, path_id: PathId, pn: u64) {
match self.in_flight_ack_frequency_frame {
Some((path, number, requested_max_ack_delay)) if path == path_id && number == pn => {
self.in_flight_ack_frequency_frame = None;
self.peer_max_ack_delay = requested_max_ack_delay;
}
_ => {}
}
}
pub(super) fn ack_frequency_received(
&mut self,
frame: &AckFrequency,
) -> Result<bool, TransportError> {
if self
.last_ack_frequency_frame
.is_some_and(|highest_sequence_nr| frame.sequence.into_inner() <= highest_sequence_nr)
{
return Ok(false);
}
self.last_ack_frequency_frame = Some(frame.sequence.into_inner());
let max_ack_delay = Duration::from_micros(frame.request_max_ack_delay.into_inner());
if max_ack_delay < TIMER_GRANULARITY {
return Err(TransportError::PROTOCOL_VIOLATION(
"Requested Max Ack Delay in ACK_FREQUENCY frame is less than min_ack_delay",
));
}
self.max_ack_delay = max_ack_delay;
Ok(true)
}
}
const MAX_RTT_ERROR: f32 = 0.2;
const MIN_AUTOMATIC_ACK_DELAY: Duration = Duration::from_millis(25);