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 = LockResult<Self::Handle>> + Send;
fn try_acquire(
&self,
) -> impl Future<Output = LockResult<Option<Self::Handle>>> + 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§
Sourcefn acquire(
&self,
timeout: Option<Duration>,
) -> impl Future<Output = LockResult<Self::Handle>> + Send
fn acquire( &self, timeout: Option<Duration>, ) -> impl Future<Output = LockResult<Self::Handle>> + Send
Acquires a semaphore ticket.
Blocks if max_count tickets are already held.
Sourcefn try_acquire(
&self,
) -> impl Future<Output = LockResult<Option<Self::Handle>>> + Send
fn try_acquire( &self, ) -> impl Future<Output = LockResult<Option<Self::Handle>>> + Send
Attempts to acquire a ticket without waiting.
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.