use std::time::{Duration, Instant};
pub const LEDBAT_TARGET_DELAY: Duration = Duration::from_millis(100);
pub const LEDBAT_MIN_CWND: u32 = 2;
pub const LEDBAT_MAX_CWND: u32 = 1000;
pub const GAIN: f64 = 0.5;
const DEFAULT_MSS: u32 = 1500;
#[derive(Debug, Clone)]
pub struct LedbatController {
cwnd: u32,
bytes_in_flight: u32,
base_delay: Option<u64>,
current_delay: u64,
target_delay_us: u64,
last_send_time: Option<Instant>,
slow_start: bool,
mss: u32,
delay_history: Vec<u64>,
max_history: usize,
slow_start_acks: u32,
}
impl Default for LedbatController {
fn default() -> Self {
Self::new()
}
}
impl LedbatController {
pub fn new() -> Self {
Self {
cwnd: LEDBAT_MIN_CWND * DEFAULT_MSS,
bytes_in_flight: 0,
base_delay: None,
current_delay: 0,
target_delay_us: LEDBAT_TARGET_DELAY.as_micros() as u64,
last_send_time: None,
slow_start: true,
mss: DEFAULT_MSS,
delay_history: Vec::with_capacity(60),
max_history: 60, slow_start_acks: 0,
}
}
pub fn with_mss(mss: u32) -> Self {
Self {
cwnd: LEDBAT_MIN_CWND * mss,
mss,
..Self::new()
}
}
pub fn with_target_delay(target_delay: Duration) -> Self {
Self {
target_delay_us: target_delay.as_micros() as u64,
..Self::new()
}
}
pub fn get_window_size(&self) -> u32 {
self.cwnd
}
pub fn get_window_packets(&self) -> u32 {
self.cwnd / self.mss
}
pub fn get_bytes_in_flight(&self) -> u32 {
self.bytes_in_flight
}
pub fn get_base_delay(&self) -> Option<Duration> {
self.base_delay.map(Duration::from_micros)
}
pub fn get_current_delay(&self) -> Duration {
Duration::from_micros(self.current_delay)
}
pub fn get_queuing_delay(&self) -> Duration {
let queuing_us = self
.current_delay
.saturating_sub(self.base_delay.unwrap_or(0));
Duration::from_micros(queuing_us)
}
pub fn can_send(&self) -> bool {
self.bytes_in_flight < self.cwnd
}
pub fn available_window(&self) -> u32 {
self.cwnd.saturating_sub(self.bytes_in_flight)
}
pub fn on_data_sent(&mut self, bytes: u32) {
self.bytes_in_flight = self.bytes_in_flight.saturating_add(bytes);
self.last_send_time = Some(Instant::now());
}
pub fn on_ack_received(&mut self, timestamp_diff: u64, bytes_acked: u32) {
self.update_delay(timestamp_diff);
self.bytes_in_flight = self.bytes_in_flight.saturating_sub(bytes_acked);
let has_enough_samples = self.delay_history.len() >= 3;
let queuing_delay_us = if has_enough_samples {
self.current_delay
.saturating_sub(self.base_delay.unwrap_or(0))
} else {
0
};
if self.slow_start {
if (has_enough_samples && queuing_delay_us > self.target_delay_us)
|| self.slow_start_acks >= 10
{
self.slow_start = false;
if has_enough_samples {
self.update_cwnd_congestion_avoidance(bytes_acked, queuing_delay_us);
}
} else {
self.slow_start_acks += 1;
self.cwnd = self.cwnd.saturating_add(bytes_acked);
}
} else {
if has_enough_samples {
self.update_cwnd_congestion_avoidance(bytes_acked, queuing_delay_us);
}
}
self.clamp_cwnd();
}
fn update_cwnd_congestion_avoidance(&mut self, bytes_acked: u32, queuing_delay_us: u64) {
let target = self.target_delay_us as f64;
let queuing = queuing_delay_us as f64;
let delay_factor = (target - queuing) / target;
let delta = GAIN * delay_factor * bytes_acked as f64;
if delta >= 0.0 {
self.cwnd = self.cwnd.saturating_add(delta as u32);
} else {
self.cwnd = self.cwnd.saturating_sub((-delta) as u32);
}
}
pub fn update_delay(&mut self, delay_us: u64) {
self.current_delay = delay_us;
self.delay_history.push(delay_us);
if self.delay_history.len() > self.max_history {
self.delay_history.remove(0);
}
self.base_delay = self.delay_history.iter().min().copied();
}
pub fn on_timeout(&mut self) {
self.cwnd = LEDBAT_MIN_CWND * self.mss;
self.bytes_in_flight = 0;
self.slow_start = true;
self.slow_start_acks = 0;
}
pub fn on_loss(&mut self) {
self.cwnd = (self.cwnd / 2).max(LEDBAT_MIN_CWND * self.mss);
self.slow_start = false;
}
pub fn reset(&mut self) {
self.cwnd = LEDBAT_MIN_CWND * self.mss;
self.bytes_in_flight = 0;
self.base_delay = None;
self.current_delay = 0;
self.last_send_time = None;
self.slow_start = true;
self.delay_history.clear();
self.slow_start_acks = 0;
}
fn clamp_cwnd(&mut self) {
let min_cwnd = LEDBAT_MIN_CWND * self.mss;
let max_cwnd = LEDBAT_MAX_CWND * self.mss;
self.cwnd = self.cwnd.clamp(min_cwnd, max_cwnd);
}
pub fn is_slow_start(&self) -> bool {
self.slow_start
}
pub fn get_target_delay(&self) -> Duration {
Duration::from_micros(self.target_delay_us)
}
pub fn time_since_last_send(&self) -> Option<Duration> {
self.last_send_time.map(|t| t.elapsed())
}
pub fn get_mss(&self) -> u32 {
self.mss
}
pub fn has_delay_samples(&self) -> bool {
!self.delay_history.is_empty()
}
pub fn delay_sample_count(&self) -> usize {
self.delay_history.len()
}
}
#[cfg(test)]
#[path = "congestion/tests.rs"]
mod tests;