pub struct BandwidthPool {
bucket: Arc<AtomicTokenBucket>,
requests: UnboundedSender<(u64, Arc<RefillWaiter>)>,
}Expand description
A shareable bandwidth pool.
Use Self::share to give it to each task requiring bandwidth limitation.
Clone is deliberately not implemented so that handing this around can not be
mistaken for creating a second pool with its own bandwidth. There is exactly one pool
per Self::new call.
Each task needs to hold a BandwidthAcquirer in order to request bandwidth permits
from this pool.
Fields§
§bucket: Arc<AtomicTokenBucket>The shared token bucket the fast path claims from.
requests: UnboundedSender<(u64, Arc<RefillWaiter>)>Ingress for acquirers that failed the fast path.
Sending a BwRequest here both enqueues it and wakes the
BandwidthRefiller
Unbounded because we never want an acquirer’s enqueue to block. the number of in-flight requests is bounded by the number of acquirers.
Implementations§
Source§impl BandwidthPool
impl BandwidthPool
Sourcepub fn new(capacity: u64) -> (BandwidthPool, BandwidthRefiller)
pub fn new(capacity: u64) -> (BandwidthPool, BandwidthRefiller)
Create a new pool that can hold up to capacity tokens, and its associated
refiller BandwidthRefiller that needs to be run in its own task.
The pool starts full and capacity tokens are immediately available to the fast
path.
Return another BandwidthPool that is the same as this one.
The tokens are not duplicated, this is so you can share the pool with other tasks/objects.
Sourcepub fn new_acquirer(&self) -> BandwidthAcquirer
pub fn new_acquirer(&self) -> BandwidthAcquirer
Return a new BandwidthAcquirer associated to this pool.
The number of tokens to acquire is chosen per
BandwidthAcquirer::poll_acquire call, so a single acquirer can be reused for
requests of different sizes.
It is through an acquirer that one can get permission to use bandwidth. See
BandwidthAcquirer::poll_acquire.
Sourcefn try_acquire(&self, tokens: u64) -> Option<Permit>
fn try_acquire(&self, tokens: u64) -> Option<Permit>
Try to take tokens from the pool without waiting.
A request above the pool capacity is clamped to it by the bucket it self so the
permit can hold less than what was asked. The caller learns what it got with
Permit::granted.
Returns Some(permit) if there were tokens available right now, or None if the
bucket is empty.
This never blocks and never enqueues. It is the fast path that the other acquisition methods use first.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for BandwidthPool
impl !UnwindSafe for BandwidthPool
impl Freeze for BandwidthPool
impl Send for BandwidthPool
impl Sync for BandwidthPool
impl Unpin for BandwidthPool
impl UnsafeUnpin for BandwidthPool
Blanket Implementations§
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