Skip to main content

TaskLock

Trait TaskLock 

Source
pub trait TaskLock: Send + Sync {
    // Required methods
    fn acquire<'life0, 'async_trait>(
        &'life0 self,
        task_id: TaskId,
        ttl: Duration,
    ) -> Pin<Box<dyn Future<Output = TaskResult<Option<LockToken>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn release<'life0, 'life1, 'async_trait>(
        &'life0 self,
        task_id: TaskId,
        token: &'life1 LockToken,
    ) -> Pin<Box<dyn Future<Output = TaskResult<bool>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn is_locked<'life0, 'async_trait>(
        &'life0 self,
        task_id: TaskId,
    ) -> Pin<Box<dyn Future<Output = TaskResult<bool>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided method
    fn extend<'life0, 'life1, 'async_trait>(
        &'life0 self,
        task_id: TaskId,
        token: &'life1 LockToken,
        ttl: Duration,
    ) -> Pin<Box<dyn Future<Output = TaskResult<bool>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

Distributed lock trait for task synchronization

§Examples

use reinhardt_tasks::{TaskLock, TaskId, LockToken};
use async_trait::async_trait;
use std::time::Duration;

struct MyLock;

#[async_trait]
impl TaskLock for MyLock {
    async fn acquire(&self, task_id: TaskId, ttl: Duration) -> reinhardt_tasks::TaskResult<Option<LockToken>> {
        // Acquire lock implementation
        Ok(Some(LockToken::generate()))
    }

    async fn release(&self, task_id: TaskId, token: &LockToken) -> reinhardt_tasks::TaskResult<bool> {
        // Release lock implementation
        Ok(true)
    }

    async fn is_locked(&self, task_id: TaskId) -> reinhardt_tasks::TaskResult<bool> {
        // Check lock status
        Ok(false)
    }
}

Required Methods§

Source

fn acquire<'life0, 'async_trait>( &'life0 self, task_id: TaskId, ttl: Duration, ) -> Pin<Box<dyn Future<Output = TaskResult<Option<LockToken>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Acquire a lock for a task

Returns Some(LockToken) if lock was acquired, None if already locked by another worker.

Source

fn release<'life0, 'life1, 'async_trait>( &'life0 self, task_id: TaskId, token: &'life1 LockToken, ) -> Pin<Box<dyn Future<Output = TaskResult<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Release a lock for a task

Returns true if the lock was released, false if the token does not match (i.e. the caller does not own the lock).

Source

fn is_locked<'life0, 'async_trait>( &'life0 self, task_id: TaskId, ) -> Pin<Box<dyn Future<Output = TaskResult<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Check if a task is locked

Provided Methods§

Source

fn extend<'life0, 'life1, 'async_trait>( &'life0 self, task_id: TaskId, token: &'life1 LockToken, ttl: Duration, ) -> Pin<Box<dyn Future<Output = TaskResult<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Extend the TTL of an existing lock

Implementors should override this with a backend-specific atomic operation to avoid race conditions where another worker could steal the lock between release and re-acquire.

Implementors§