pub struct ConcurrencyLimiter { /* private fields */ }Expand description
Limits concurrent backend operations and tracks in-flight count.
Permits are acquired with acquire or
acquire_bulk and automatically returned when
the ConcurrencyPermit is dropped.
Bulk operations use a separate budget semaphore that limits how many execution slots they may occupy. This is intended as a safe operating point — below this level there should be little-to-no performance degradation, leaving room for more tasks to be admitted via the queue before rejection is necessary.
Implementations§
Source§impl ConcurrencyLimiter
impl ConcurrencyLimiter
Sourcepub fn new(max: u32) -> Self
pub fn new(max: u32) -> Self
Creates a new limiter with the given maximum number of permits.
By default the queue depth is zero, preserving the original try-or-reject behavior. Use
with_queue to enable bounded waiting.
The bulk budget defaults to 100% of max (no restriction); use
with_bulk to set a safe operating point for bulk traffic.
Sourcepub fn with_queue(self, size: u32) -> Self
pub fn with_queue(self, size: u32) -> Self
Enables bounded waiting when all execution permits are held.
Up to size additional callers may park in acquire
waiting for a permit. Callers beyond that are rejected immediately.
Sourcepub fn with_timeout(self, timeout: Duration) -> Self
pub fn with_timeout(self, timeout: Duration) -> Self
Sets the maximum time a caller may wait for a permit.
Applies to both acquire (when parked in the
queue) and acquire_bulk (waiting for the
bulk and execution semaphores). Defaults to 1 second.
Sourcepub fn with_bulk(self, percent: u32) -> Self
pub fn with_bulk(self, percent: u32) -> Self
Sets the bulk concurrency budget as a percentage of max.
percent is clamped to 1..=100. At 100 (the default), bulk
operations can use all execution slots. Lower values set a safe
operating point below which there is little-to-no performance
degradation, allowing more tasks to queue before rejection is
necessary — e.g. 60 means bulk operations can hold at most 60%
of permits.
Sourcepub async fn acquire(&self) -> Result<ConcurrencyPermit>
pub async fn acquire(&self) -> Result<ConcurrencyPermit>
Acquires a single concurrency permit, waiting if necessary.
If a permit is free, returns immediately without touching the
queue. Otherwise, acquires a queue ticket (bounded by the queue
depth) and waits up to the configured timeout. Returns
Error::AtCapacity if the queue is full or on timeout.
Sourcepub fn try_acquire(&self) -> Result<ConcurrencyPermit>
pub fn try_acquire(&self) -> Result<ConcurrencyPermit>
Tries to acquire a single permit without waiting.
Returns Error::AtCapacity when no permits are available.
Sourcepub async fn acquire_bulk(&self) -> Result<ConcurrencyPermit>
pub async fn acquire_bulk(&self) -> Result<ConcurrencyPermit>
Acquires a single permit for a bulk operation, waiting if necessary.
Bulk operations are bounded by the bulk budget — a safe operating
point below which there is little-to-no performance degradation.
Both the bulk semaphore and the inner execution semaphore are
acquired under a single timeout deadline configured via
with_timeout.
Returns Error::AtCapacity on timeout or when max is zero.
Sourcepub fn available_permits(&self) -> u32
pub fn available_permits(&self) -> u32
Returns the number of permits currently available.
Sourcepub fn used_permits(&self) -> u32
pub fn used_permits(&self) -> u32
Returns the number of permits currently held.
Sourcepub fn total_permits(&self) -> u32
pub fn total_permits(&self) -> u32
Returns the total number of permits.
Sourcepub fn queued_permits(&self) -> u32
pub fn queued_permits(&self) -> u32
Returns the number of callers currently waiting in the queue.
Sourcepub fn total_queue(&self) -> u32
pub fn total_queue(&self) -> u32
Returns the configured queue capacity.
Sourcepub fn used_bulk_permits(&self) -> u32
pub fn used_bulk_permits(&self) -> u32
Returns the number of bulk permits currently held.
Sourcepub fn total_bulk(&self) -> u32
pub fn total_bulk(&self) -> u32
Returns the bulk concurrency budget.
Trait Implementations§
Source§impl Clone for ConcurrencyLimiter
impl Clone for ConcurrencyLimiter
Source§fn clone(&self) -> ConcurrencyLimiter
fn clone(&self) -> ConcurrencyLimiter
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for ConcurrencyLimiter
impl RefUnwindSafe for ConcurrencyLimiter
impl Send for ConcurrencyLimiter
impl Sync for ConcurrencyLimiter
impl Unpin for ConcurrencyLimiter
impl UnsafeUnpin for ConcurrencyLimiter
impl UnwindSafe for ConcurrencyLimiter
Blanket Implementations§
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].