pub struct FlowControl { /* private fields */ }Expand description
Window-based flow control state for managing reliable data transmission.
This struct encapsulates the state needed for sliding window flow control, including the current window size and tracking of in-transit reliable data.
Implementations§
Source§impl FlowControl
impl FlowControl
Sourcepub fn new(config: &Config) -> Self
pub fn new(config: &Config) -> Self
Creates a new flow control instance with configuration defaults.
§Arguments
config- Configuration containing initial window size and limits
§Example
use bitfold_peer::flow_control::FlowControl;
use bitfold_core::config::Config;
let config = Config::default();
let flow_control = FlowControl::new(&config);Sourcepub fn window_size(&self) -> u32
pub fn window_size(&self) -> u32
Returns the current window size (in packets).
The window size represents the maximum number of packets that can be in flight (unacknowledged) at any given time.
Sourcepub fn reliable_data_in_transit(&self) -> u32
pub fn reliable_data_in_transit(&self) -> u32
Returns the amount of reliable data currently in transit (in bytes).
This tracks the total bytes of reliable data that have been sent but not yet acknowledged by the receiver.
Sourcepub fn set_window_size(&mut self, config: &Config, window_size: u32)
pub fn set_window_size(&mut self, config: &Config, window_size: u32)
Sets the window size (for negotiation during handshake).
The provided window size will be clamped to the configured min/max bounds.
§Arguments
config- Configuration containing min and max window size limitswindow_size- Desired window size (will be clamped to min/max)
Sourcepub fn record_reliable_data_sent(&mut self, data_size: u32)
pub fn record_reliable_data_sent(&mut self, data_size: u32)
Records reliable data being sent (adds to in-transit counter).
Call this method whenever reliable data is transmitted to track how much data is waiting for acknowledgment.
§Arguments
data_size- Size of the data being sent, in bytes
Sourcepub fn record_reliable_data_acked(&mut self, data_size: u32)
pub fn record_reliable_data_acked(&mut self, data_size: u32)
Records reliable data being acknowledged (removes from in-transit counter).
Call this method when an acknowledgment is received to reduce the amount of tracked in-transit data.
§Arguments
data_size- Size of the data being acknowledged, in bytes
Sourcepub fn can_send_reliable(&self, config: &Config, packets_in_flight: u16) -> bool
pub fn can_send_reliable(&self, config: &Config, packets_in_flight: u16) -> bool
Checks if we can send more data based on window-based flow control.
Returns true if we have room in the window, false if the window is full.
When window flow control is disabled, this falls back to checking a simple
packet count limit.
§Arguments
config- Configuration containing window control settingspackets_in_flight- Current number of unacknowledged packets (used when window flow control is disabled)
§Returns
true if more reliable data can be sent, false if the window is full
Sourcepub fn adjust_window_size(
&mut self,
config: &Config,
loss_rate: f32,
rtt_ms: u32,
)
pub fn adjust_window_size( &mut self, config: &Config, loss_rate: f32, rtt_ms: u32, )
Dynamically adjusts the window size based on network conditions.
This should be called periodically to adapt the window size to changing network conditions. The window grows during good conditions and shrinks during poor conditions.
§Adjustment Logic
- Increase: When loss rate < 1% and RTT < 200ms, grow window by ~3% (1/32)
- Decrease: When loss rate > 5% or RTT > 500ms, shrink window by ~6% (1/16)
- No change: Otherwise maintain current window size
The window size is always clamped to the configured min/max bounds.
§Arguments
config- Configuration containing window control settings and limitsloss_rate- Current packet loss rate (0.0 to 1.0)rtt_ms- Current round-trip time in milliseconds
Trait Implementations§
Source§impl Clone for FlowControl
impl Clone for FlowControl
Source§fn clone(&self) -> FlowControl
fn clone(&self) -> FlowControl
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more