pub struct Counter(/* private fields */);Expand description
A shared count with an asynchronous capacity notification.
Clones use the same count and capacity, but each clone registers its own waiting task.
Implementations§
Source§impl Counter
impl Counter
Sourcepub fn get(&self) -> CounterGuard
pub fn get(&self) -> CounterGuard
Sourcepub fn try_get(&self) -> Option<CounterGuard>
pub fn try_get(&self) -> Option<CounterGuard>
Acquires one count if the counter has free capacity.
Sourcepub async fn acquire(&self) -> CounterGuard
pub async fn acquire(&self) -> CounterGuard
Waits until the counter has free capacity and acquires one count.
The count is taken in the same poll that observes free capacity, so tasks acquiring concurrently never exceed the capacity. Concurrent waiters must use separate clones, each clone wakes only its most recent waiting task. Cancelling the future acquires nothing.
Sourcepub fn set_capacity(&self, cap: usize)
pub fn set_capacity(&self, cap: usize)
Changes the capacity and wakes waiting tasks.
Sourcepub fn is_available(&self) -> bool
pub fn is_available(&self) -> bool
Returns true if another count can be acquired without exceeding the capacity.
Sourcepub async fn available(&self)
pub async fn available(&self)
Waits until the counter has free capacity.
Returns immediately if there is capacity available. Otherwise, registers the current task for wakeup and waits until a slot is freed.
This does not reserve the slot: another task may take it before a
later get, unless get is called without awaiting in
between. Use acquire to wait and take a slot at once.
Waits until the counter reaches its capacity (i.e., becomes unavailable).
Sourcepub fn bind(&self, ctx: &BindCtx<'_>)
pub fn bind(&self, ctx: &BindCtx<'_>)
Subscribes ctx to capacity changes.
Services whose readiness depends on this counter must call this in
Service::bind, otherwise their cached
readiness is not invalidated when the counter fills or frees a slot.