pub struct BandwidthThrottle { /* private fields */ }Expand description
Tracks bandwidth usage and enforces bandwidth limits.
This structure maintains a sliding window of bandwidth consumption, resetting counters every second to provide accurate per-second limiting.
Implementations§
Source§impl BandwidthThrottle
impl BandwidthThrottle
Sourcepub fn new(
outgoing_limit: u32,
incoming_limit: u32,
start_time: Instant,
) -> Self
pub fn new( outgoing_limit: u32, incoming_limit: u32, start_time: Instant, ) -> Self
Creates a new bandwidth throttle with the specified limits.
§Arguments
outgoing_limit- Maximum bytes per second for outgoing traffic (0 = unlimited)incoming_limit- Maximum bytes per second for incoming traffic (0 = unlimited)start_time- Initial timestamp for the bandwidth window
Sourcepub fn update_bandwidth_window(&mut self, time: Instant) -> bool
pub fn update_bandwidth_window(&mut self, time: Instant) -> bool
Updates bandwidth tracking window, resetting counters if the window has expired.
This should be called periodically (e.g., before each send/receive operation) to ensure accurate bandwidth tracking.
§Returns
Returns true if the window was reset, false otherwise.
Sourcepub fn record_bytes_sent(&mut self, bytes: u32)
pub fn record_bytes_sent(&mut self, bytes: u32)
Records bytes sent for bandwidth tracking.
This increments the send counter using saturating addition to prevent overflow.
Sourcepub fn record_bytes_received(&mut self, bytes: u32)
pub fn record_bytes_received(&mut self, bytes: u32)
Records bytes received for bandwidth tracking.
This increments the receive counter using saturating addition to prevent overflow.
Sourcepub fn can_send_within_bandwidth(&self) -> bool
pub fn can_send_within_bandwidth(&self) -> bool
Checks if we can send based on the outgoing bandwidth limit.
§Returns
Returns true if we’re under the limit or if throttling is disabled (limit == 0).
Sourcepub fn bandwidth_utilization(&self) -> f32
pub fn bandwidth_utilization(&self) -> f32
Returns current outgoing bandwidth utilization (0.0 to 1.0+).
§Returns
0.0if bandwidth limiting is disabled0.5if at 50% of the limit1.0if at the limit>1.0if over the limit
Sourcepub fn can_receive_within_bandwidth(&self) -> bool
pub fn can_receive_within_bandwidth(&self) -> bool
Checks if we can receive based on the incoming bandwidth limit.
§Returns
Returns true if we’re under the limit or if throttling is disabled (limit == 0).
Sourcepub fn incoming_bandwidth_utilization(&self) -> f32
pub fn incoming_bandwidth_utilization(&self) -> f32
Returns current incoming bandwidth utilization (0.0 to 1.0+).
§Returns
0.0if bandwidth limiting is disabled0.5if at 50% of the limit1.0if at the limit>1.0if over the limit
Sourcepub fn set_outgoing_bandwidth_limit(&mut self, limit: u32)
pub fn set_outgoing_bandwidth_limit(&mut self, limit: u32)
Updates the outgoing bandwidth limit.
§Arguments
limit- New outgoing bandwidth limit in bytes per second (0 = unlimited)
Sourcepub fn set_incoming_bandwidth_limit(&mut self, limit: u32)
pub fn set_incoming_bandwidth_limit(&mut self, limit: u32)
Updates the incoming bandwidth limit.
§Arguments
limit- New incoming bandwidth limit in bytes per second (0 = unlimited)
Sourcepub fn outgoing_bandwidth_limit(&self) -> u32
pub fn outgoing_bandwidth_limit(&self) -> u32
Returns the current outgoing bandwidth limit.
Sourcepub fn incoming_bandwidth_limit(&self) -> u32
pub fn incoming_bandwidth_limit(&self) -> u32
Returns the current incoming bandwidth limit.
Sourcepub fn bytes_sent_this_window(&self) -> u32
pub fn bytes_sent_this_window(&self) -> u32
Returns the bytes sent in the current window.
Sourcepub fn bytes_received_this_window(&self) -> u32
pub fn bytes_received_this_window(&self) -> u32
Returns the bytes received in the current window.