struct DirectionState {
acquirer: BandwidthAcquirer,
max_chunk: NonZero<usize>,
}Expand description
Rate-limiting state for a single direction.
It has everything needed to rate limit one direction that is a BandwidthAcquirer
and an optional maximum chunk.
This is used by the GlobalRateLimitedReader and GlobalRateLimitedWriter as
they share that same behavior for each direction (read and write).
Fields§
§acquirer: BandwidthAcquirerAcquirer used to get a Permit from the pool for each poll.
max_chunk: NonZero<usize>Cap on how many tokens a single IO can request.
An IO requests at most this many tokens as long as the buffer is bigger.
Implementations§
Source§impl DirectionState
impl DirectionState
Sourcefn new(acquirer: BandwidthAcquirer, max_chunk: NonZero<usize>) -> Self
fn new(acquirer: BandwidthAcquirer, max_chunk: NonZero<usize>) -> Self
Constructor.
Sourcefn poll_acquire(
&mut self,
cx: &mut Context<'_>,
tokens: usize,
) -> Poll<Result<Permit, Error>>
fn poll_acquire( &mut self, cx: &mut Context<'_>, tokens: usize, ) -> Poll<Result<Permit, Error>>
Acquire a permit for an IO of the given amount of tokens.
The request is capped to the state’s max chunk. The grant itself is capped
to the pool capacity so the returned Permit can hold less than tokens.
The Permit is handed to the caller rather than kept here so that it lives
exactly as long as the IO attempt it is for. If the underlying IO turns out to be
Poll::Pending, the caller drops it and the tokens are refunded to the pool
instead of being parked for as long as the connection is not ready. That matters
with many connections as we don’t want to hold off ready connections on already
allocated permits for non ready connections.
The cost is that the caller re-acquires on the next poll rather than resuming with what it already had. It is by design.
Sourcefn commit(permit: Permit, tokens: usize)
fn commit(permit: Permit, tokens: usize)
Claim tokens on permit after a successful IO.
tokens must not exceed what the permit was granted. Callers get that by capping
the IO to Permit::granted.
The permit is consumed so whatever is left unclaimed is refunded on drop.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for DirectionState
impl !UnwindSafe for DirectionState
impl Freeze for DirectionState
impl Send for DirectionState
impl Sync for DirectionState
impl Unpin for DirectionState
impl UnsafeUnpin for DirectionState
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