use super::{
DEFAULT_IDLE_TIMEOUT, DEFAULT_INITIAL_RTO, DEFAULT_KEEPALIVE_INTERVAL, MAX_RETRANSMIT_ATTEMPTS,
MAX_RTO, MIN_RTO,
};
use std::time::{Duration, Instant};
#[derive(Debug, Clone)]
pub struct RetransmitScheduler {
base_rto: Duration,
current_rto: Duration,
timeout_count: u32,
max_attempts: u32,
backoff_multiplier: f64,
max_backoff: f64,
}
impl Default for RetransmitScheduler {
fn default() -> Self {
Self::new()
}
}
impl RetransmitScheduler {
pub fn new() -> Self {
Self {
base_rto: DEFAULT_INITIAL_RTO,
current_rto: DEFAULT_INITIAL_RTO,
timeout_count: 0,
max_attempts: MAX_RETRANSMIT_ATTEMPTS,
backoff_multiplier: 2.0,
max_backoff: 64.0,
}
}
pub fn with_rto(rto: Duration) -> Self {
Self {
base_rto: rto,
current_rto: rto,
..Self::new()
}
}
pub fn rto(&self) -> Duration {
self.current_rto
}
pub fn base_rto(&self) -> Duration {
self.base_rto
}
pub fn timeout_count(&self) -> u32 {
self.timeout_count
}
pub fn is_max_attempts_reached(&self) -> bool {
self.timeout_count >= self.max_attempts
}
pub fn on_timeout(&mut self) {
self.timeout_count += 1;
let backoff = self.backoff_multiplier.powi(self.timeout_count as i32);
let clamped_backoff = backoff.min(self.max_backoff);
let new_rto_us = (self.base_rto.as_micros() as f64 * clamped_backoff) as u64;
self.current_rto = Duration::from_micros(new_rto_us).clamp(MIN_RTO, MAX_RTO);
}
pub fn on_ack_received(&mut self) {
self.timeout_count = 0;
self.current_rto = self.base_rto;
}
pub fn update_rto(&mut self, srtt: Duration, rttvar: Duration) {
let new_rto = srtt + 4 * rttvar;
self.base_rto = new_rto.clamp(MIN_RTO, MAX_RTO);
if self.timeout_count == 0 {
self.current_rto = self.base_rto;
}
}
pub fn set_max_attempts(&mut self, max: u32) {
self.max_attempts = max;
}
pub fn reset(&mut self) {
self.current_rto = self.base_rto;
self.timeout_count = 0;
}
}
#[derive(Debug, Clone)]
pub struct KeepaliveManager {
interval: Duration,
last_keepalive: Option<Instant>,
last_activity: Option<Instant>,
enabled: bool,
}
impl Default for KeepaliveManager {
fn default() -> Self {
Self::new()
}
}
impl KeepaliveManager {
pub fn new() -> Self {
Self {
interval: DEFAULT_KEEPALIVE_INTERVAL,
last_keepalive: None,
last_activity: None,
enabled: true,
}
}
pub fn with_interval(interval: Duration) -> Self {
Self {
interval,
..Self::new()
}
}
pub fn interval(&self) -> Duration {
self.interval
}
pub fn set_interval(&mut self, interval: Duration) {
self.interval = interval;
}
pub fn is_enabled(&self) -> bool {
self.enabled
}
pub fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled;
}
pub fn record_activity(&mut self) {
self.record_activity_at(Instant::now());
}
pub fn record_activity_at(&mut self, now: Instant) {
self.last_activity = Some(now);
}
pub fn record_keepalive_sent(&mut self) {
self.record_keepalive_sent_at(Instant::now());
}
pub fn record_keepalive_sent_at(&mut self, now: Instant) {
self.last_keepalive = Some(now);
}
pub fn should_send_keepalive(&self) -> bool {
self.should_send_keepalive_at(Instant::now())
}
pub fn should_send_keepalive_at(&self, now: Instant) -> bool {
if !self.enabled {
return false;
}
self.last_event()
.is_none_or(|last| now.saturating_duration_since(last) >= self.interval)
}
pub fn next_keepalive(&self) -> Option<Duration> {
self.next_keepalive_at(Instant::now())
}
pub fn next_keepalive_at(&self, now: Instant) -> Option<Duration> {
if !self.enabled {
return None;
}
Some(self.last_event().map_or(self.interval, |last| {
self.interval
.saturating_sub(now.saturating_duration_since(last))
}))
}
fn last_event(&self) -> Option<Instant> {
match (self.last_activity, self.last_keepalive) {
(Some(activity), Some(keepalive)) => Some(activity.max(keepalive)),
(Some(activity), None) => Some(activity),
(None, Some(keepalive)) => Some(keepalive),
(None, None) => None,
}
}
pub fn reset(&mut self) {
self.last_keepalive = None;
self.last_activity = None;
}
}
#[derive(Debug, Clone)]
pub struct IdleTimeoutDetector {
timeout: Duration,
last_activity: Option<Instant>,
enabled: bool,
}
impl Default for IdleTimeoutDetector {
fn default() -> Self {
Self::new()
}
}
impl IdleTimeoutDetector {
pub fn new() -> Self {
Self {
timeout: DEFAULT_IDLE_TIMEOUT,
last_activity: None,
enabled: true,
}
}
pub fn with_timeout(timeout: Duration) -> Self {
Self {
timeout,
..Self::new()
}
}
pub fn timeout(&self) -> Duration {
self.timeout
}
pub fn set_timeout(&mut self, timeout: Duration) {
self.timeout = timeout;
}
pub fn is_enabled(&self) -> bool {
self.enabled
}
pub fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled;
}
pub fn record_activity(&mut self) {
self.last_activity = Some(Instant::now());
}
pub fn is_timeout(&self) -> bool {
if !self.enabled {
return false;
}
if let Some(last_activity) = self.last_activity {
last_activity.elapsed() >= self.timeout
} else {
true
}
}
pub fn idle_time(&self) -> Duration {
self.last_activity.map_or(Duration::ZERO, |t| t.elapsed())
}
pub fn remaining_time(&self) -> Option<Duration> {
if !self.enabled {
return None;
}
let idle = self.idle_time();
if idle >= self.timeout {
Some(Duration::ZERO)
} else {
Some(self.timeout - idle)
}
}
pub fn reset(&mut self) {
self.last_activity = Some(Instant::now());
}
}