rat_congestion 0.1.0

High-performance congestion control algorithms for network protocols
Documentation
//! Congestion control events and related types.
//!
//! This module defines the events that drive congestion control algorithms,
//! including packet acknowledgments, losses, and explicit congestion notifications.

use std::time::Duration;

/// Events that trigger congestion control algorithm updates.
///
/// These events represent the primary signals that congestion control algorithms
/// use to adjust their behavior and maintain optimal network performance.
#[derive(Debug, Clone, PartialEq)]
pub enum CongestionEvent {
    /// A packet was successfully acknowledged by the receiver.
    ///
    /// This event indicates successful data delivery and provides timing
    /// information for bandwidth and RTT estimation.
    PacketAcked {
        /// Sequence number or identifier of the acknowledged packet
        packet_number: u64,
        /// Number of bytes in the acknowledged packet
        bytes: u64,
        /// Round-trip time measured for this packet
        rtt: Duration,
    },
    
    /// A packet was detected as lost.
    ///
    /// This event triggers congestion avoidance mechanisms and typically
    /// results in reducing the congestion window.
    PacketLost {
        /// Sequence number or identifier of the lost packet
        packet_number: u64,
        /// Number of bytes in the lost packet
        bytes: u64,
    },
    
    /// Explicit Congestion Notification (ECN) was received.
    ///
    /// This event indicates network congestion without packet loss,
    /// allowing for more gentle congestion response.
    ExplicitCongestionNotification,
}

impl CongestionEvent {
    /// Returns the number of bytes associated with this event, if applicable.
    pub fn bytes(&self) -> Option<u64> {
        match self {
            CongestionEvent::PacketAcked { bytes, .. } => Some(*bytes),
            CongestionEvent::PacketLost { bytes, .. } => Some(*bytes),
            CongestionEvent::ExplicitCongestionNotification => None,
        }
    }
    
    /// Returns the packet number associated with this event, if applicable.
    pub fn packet_number(&self) -> Option<u64> {
        match self {
            CongestionEvent::PacketAcked { packet_number, .. } => Some(*packet_number),
            CongestionEvent::PacketLost { packet_number, .. } => Some(*packet_number),
            CongestionEvent::ExplicitCongestionNotification => None,
        }
    }
    
    /// Returns the RTT associated with this event, if applicable.
    pub fn rtt(&self) -> Option<Duration> {
        match self {
            CongestionEvent::PacketAcked { rtt, .. } => Some(*rtt),
            _ => None,
        }
    }
    
    /// Returns true if this event indicates successful data delivery.
    pub fn is_positive(&self) -> bool {
        matches!(self, CongestionEvent::PacketAcked { .. })
    }
    
    /// Returns true if this event indicates congestion or loss.
    pub fn is_negative(&self) -> bool {
        matches!(
            self,
            CongestionEvent::PacketLost { .. } | CongestionEvent::ExplicitCongestionNotification
        )
    }
}