pub struct BandwidthBudgetManager { /* private fields */ }Expand description
Per-peer token-bucket bandwidth manager.
Call refill or refill_all periodically (or lazily before each
consume call) to add tokens, then use try_consume_upload /
try_consume_download to request bandwidth.
Implementations§
Source§impl BandwidthBudgetManager
impl BandwidthBudgetManager
Sourcepub fn new(config: BudgetConfig) -> Self
pub fn new(config: BudgetConfig) -> Self
Creates a new manager with the supplied configuration.
Sourcepub fn register_peer(&mut self, peer_id: &str, now: u64)
pub fn register_peer(&mut self, peer_id: &str, now: u64)
Registers a peer using the default quota.
If the peer is already registered this is a no-op.
Sourcepub fn register_peer_with_quota(
&mut self,
peer_id: &str,
quota: BandwidthQuota,
now: u64,
)
pub fn register_peer_with_quota( &mut self, peer_id: &str, quota: BandwidthQuota, now: u64, )
Registers a peer with a custom quota, overriding the default.
If the peer already exists its quota is replaced and tokens are re-initialised.
Sourcepub fn refill(&mut self, peer_id: &str, now: u64)
pub fn refill(&mut self, peer_id: &str, now: u64)
Refills the token bucket for a single peer based on elapsed time.
Does nothing if the peer is not registered or now is not later than
the peer’s last_refill timestamp.
Sourcepub fn refill_all(&mut self, now: u64)
pub fn refill_all(&mut self, now: u64)
Refills token buckets for all registered peers.
Sourcepub fn try_consume_upload(
&mut self,
peer_id: &str,
bytes: u64,
now: u64,
) -> bool
pub fn try_consume_upload( &mut self, peer_id: &str, bytes: u64, now: u64, ) -> bool
Attempts to consume bytes of upload bandwidth for peer_id.
Returns true and deducts tokens when:
- The peer is registered,
- The peer has enough upload tokens,
- The global upload cap has not been reached.
Returns false (and does not deduct tokens) in all other cases.
Sourcepub fn try_consume_download(
&mut self,
peer_id: &str,
bytes: u64,
now: u64,
) -> bool
pub fn try_consume_download( &mut self, peer_id: &str, bytes: u64, now: u64, ) -> bool
Attempts to consume bytes of download bandwidth for peer_id.
Returns true and deducts tokens when:
- The peer is registered,
- The peer has enough download tokens,
- The global download cap has not been reached.
Returns false (and does not deduct tokens) in all other cases.
Sourcepub fn remaining_upload(&self, peer_id: &str) -> u64
pub fn remaining_upload(&self, peer_id: &str) -> u64
Returns the number of remaining upload tokens for peer_id, floored to 0.
Returns 0 for unknown peers.
Sourcepub fn remaining_download(&self, peer_id: &str) -> u64
pub fn remaining_download(&self, peer_id: &str) -> u64
Returns the number of remaining download tokens for peer_id, floored to 0.
Returns 0 for unknown peers.
Sourcepub fn remove_peer(&mut self, peer_id: &str) -> bool
pub fn remove_peer(&mut self, peer_id: &str) -> bool
Removes a peer from the manager.
Returns true if the peer existed, false otherwise.
Sourcepub fn peer_count(&self) -> usize
pub fn peer_count(&self) -> usize
Returns the number of currently registered peers.
Sourcepub fn stats(&self) -> &BudgetStats
pub fn stats(&self) -> &BudgetStats
Returns a reference to the aggregate statistics.
Auto Trait Implementations§
impl Freeze for BandwidthBudgetManager
impl RefUnwindSafe for BandwidthBudgetManager
impl Send for BandwidthBudgetManager
impl Sync for BandwidthBudgetManager
impl Unpin for BandwidthBudgetManager
impl UnsafeUnpin for BandwidthBudgetManager
impl UnwindSafe for BandwidthBudgetManager
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more