pub trait DistributedSemaphore: Send + Sync {
type Handle: LockHandle + Send;
// Required methods
fn name(&self) -> &str;
fn max_count(&self) -> u32;
fn acquire(
&self,
timeout: Option<Duration>,
) -> impl Future<Output = Result<Self::Handle, LockError>> + Send;
fn try_acquire(
&self,
) -> impl Future<Output = Result<Option<Self::Handle>, LockError>> + Send;
}Expand description
A distributed counting semaphore.
Allows up to max_count processes to hold the semaphore concurrently.
Useful for rate limiting or resource pooling.
§Example
ⓘ
// Create a semaphore allowing 5 concurrent database connections
let semaphore = provider.create_semaphore("db-pool", 5);
// Acquire a "ticket"
let ticket = semaphore.acquire(None).await?;
// Use the limited resource
use_database_connection().await;
// Release the ticket
ticket.release().await?;Required Associated Types§
Sourcetype Handle: LockHandle + Send
type Handle: LockHandle + Send
Handle type for semaphore tickets.
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.