pub trait DistributedLock:
Send
+ Sync
+ Debug {
// Required methods
fn acquire<'life0, 'life1, 'async_trait>(
&'life0 self,
resource: &'life1 str,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = StateResult<LockGuard>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn try_acquire<'life0, 'life1, 'async_trait>(
&'life0 self,
resource: &'life1 str,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = StateResult<Option<LockGuard>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn release_internal<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
resource: &'life1 str,
token: &'life2 LockToken,
) -> Pin<Box<dyn Future<Output = StateResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait;
fn is_locked<'life0, 'life1, 'async_trait>(
&'life0 self,
resource: &'life1 str,
) -> Pin<Box<dyn Future<Output = StateResult<bool>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn extend<'life0, 'life1, 'async_trait>(
&'life0 self,
guard: &'life1 mut LockGuard,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = StateResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn get_lock_info<'life0, 'life1, 'async_trait>(
&'life0 self,
resource: &'life1 str,
) -> Pin<Box<dyn Future<Output = StateResult<Option<LockToken>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
// Provided method
fn release<'life0, 'async_trait>(
&'life0 self,
guard: LockGuard,
) -> Pin<Box<dyn Future<Output = StateResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
}Expand description
Distributed lock trait
Defines the interface for distributed locking implementations.
All operations are async and use the RAII pattern via LockGuard.
Required Methods§
Sourcefn acquire<'life0, 'life1, 'async_trait>(
&'life0 self,
resource: &'life1 str,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = StateResult<LockGuard>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn acquire<'life0, 'life1, 'async_trait>(
&'life0 self,
resource: &'life1 str,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = StateResult<LockGuard>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Acquire a lock for the given resource
Blocks until the lock is acquired or an error occurs. The lock will automatically expire after the TTL.
§Arguments
resource- Name of the resource to lockttl- Time-to-live for the lock
§Returns
Ok(LockGuard)- Lock acquired successfullyErr(StateError)- Failed to acquire lock
§Example
let guard = lock.acquire("checkpoint", Duration::from_secs(30)).await?;
// Critical section
drop(guard); // Lock releasedSourcefn try_acquire<'life0, 'life1, 'async_trait>(
&'life0 self,
resource: &'life1 str,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = StateResult<Option<LockGuard>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn try_acquire<'life0, 'life1, 'async_trait>(
&'life0 self,
resource: &'life1 str,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = StateResult<Option<LockGuard>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Try to acquire a lock without blocking
Returns immediately with success or failure. Useful for non-blocking scenarios.
§Arguments
resource- Name of the resource to lockttl- Time-to-live for the lock
§Returns
Ok(Some(LockGuard))- Lock acquiredOk(None)- Lock already held by another instanceErr(StateError)- Backend error
Sourcefn release_internal<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
resource: &'life1 str,
token: &'life2 LockToken,
) -> Pin<Box<dyn Future<Output = StateResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn release_internal<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
resource: &'life1 str,
token: &'life2 LockToken,
) -> Pin<Box<dyn Future<Output = StateResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Internal release method (used by Drop)
Sourcefn is_locked<'life0, 'life1, 'async_trait>(
&'life0 self,
resource: &'life1 str,
) -> Pin<Box<dyn Future<Output = StateResult<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn is_locked<'life0, 'life1, 'async_trait>(
&'life0 self,
resource: &'life1 str,
) -> Pin<Box<dyn Future<Output = StateResult<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Sourcefn extend<'life0, 'life1, 'async_trait>(
&'life0 self,
guard: &'life1 mut LockGuard,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = StateResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn extend<'life0, 'life1, 'async_trait>(
&'life0 self,
guard: &'life1 mut LockGuard,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = StateResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Extend the TTL of an existing lock
Useful for long-running operations that might exceed the initial TTL. The guard’s TTL is updated on success.
§Arguments
guard- The lock guard to extendttl- New TTL from current time
§Returns
Ok(())- TTL extendedErr(StateError)- Failed to extend (token mismatch, lock expired)
§Example
let mut guard = lock.acquire("long_operation", Duration::from_secs(30)).await?;
// Long operation...
tokio::time::sleep(Duration::from_secs(20)).await;
// Extend before expiration
lock.extend(&mut guard, Duration::from_secs(30)).await?;Sourcefn get_lock_info<'life0, 'life1, 'async_trait>(
&'life0 self,
resource: &'life1 str,
) -> Pin<Box<dyn Future<Output = StateResult<Option<LockToken>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get_lock_info<'life0, 'life1, 'async_trait>(
&'life0 self,
resource: &'life1 str,
) -> Pin<Box<dyn Future<Output = StateResult<Option<LockToken>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Provided Methods§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".