Skip to main content

BandwidthThrottle

Struct BandwidthThrottle 

Source
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

Source

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
Source

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.

Source

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.

Source

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.

Source

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).

Source

pub fn bandwidth_utilization(&self) -> f32

Returns current outgoing bandwidth utilization (0.0 to 1.0+).

§Returns
  • 0.0 if bandwidth limiting is disabled
  • 0.5 if at 50% of the limit
  • 1.0 if at the limit
  • >1.0 if over the limit
Source

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).

Source

pub fn incoming_bandwidth_utilization(&self) -> f32

Returns current incoming bandwidth utilization (0.0 to 1.0+).

§Returns
  • 0.0 if bandwidth limiting is disabled
  • 0.5 if at 50% of the limit
  • 1.0 if at the limit
  • >1.0 if over the limit
Source

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)
Source

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)
Source

pub fn outgoing_bandwidth_limit(&self) -> u32

Returns the current outgoing bandwidth limit.

Source

pub fn incoming_bandwidth_limit(&self) -> u32

Returns the current incoming bandwidth limit.

Source

pub fn bytes_sent_this_window(&self) -> u32

Returns the bytes sent in the current window.

Source

pub fn bytes_received_this_window(&self) -> u32

Returns the bytes received in the current window.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more