Skip to main content

DistributedLock

Trait DistributedLock 

Source
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§

Source

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 lock
  • ttl - Time-to-live for the lock
§Returns
  • Ok(LockGuard) - Lock acquired successfully
  • Err(StateError) - Failed to acquire lock
§Example
let guard = lock.acquire("checkpoint", Duration::from_secs(30)).await?;
// Critical section
drop(guard); // Lock released
Source

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 lock
  • ttl - Time-to-live for the lock
§Returns
  • Ok(Some(LockGuard)) - Lock acquired
  • Ok(None) - Lock already held by another instance
  • Err(StateError) - Backend error
Source

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)

Source

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,

Check if a resource is currently locked

§Arguments
  • resource - Name of the resource to check
§Returns
  • Ok(true) - Resource is locked
  • Ok(false) - Resource is not locked
  • Err(StateError) - Backend error
Source

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 extend
  • ttl - New TTL from current time
§Returns
  • Ok(()) - TTL extended
  • Err(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?;
Source

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,

Get information about who holds a lock

Returns the token of the current lock holder, if any.

§Arguments
  • resource - Name of the resource to query
§Returns
  • Ok(Some(LockToken)) - Lock is held
  • Ok(None) - Lock is not held
  • Err(StateError) - Backend error

Provided Methods§

Source

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,

Release a lock

Normally not needed as locks are released automatically when the guard is dropped. This is provided for explicit control.

§Arguments
  • guard - The lock guard to release
§Returns
  • Ok(()) - Lock released
  • Err(StateError) - Failed to release (token mismatch, already released)

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§