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
Acquires one count and returns a guard that releases it on drop.
This does not wait for capacity; call available
first when exceeding the configured capacity is not acceptable.
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.
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.