pub trait DistributedLock: Send + Sync {
// Required methods
fn acquire<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LockError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn release<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 str,
token: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<(), LockError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait;
// Provided method
fn renew<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_key: &'life1 str,
_token: &'life2 str,
_ttl: Duration,
) -> Pin<Box<dyn Future<Output = Result<(), LockError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
}Expand description
Distributed lock abstraction.
acquire returns an ownership token that must be passed to release,
so a lock can only be released by the process that holds it.
Required Methods§
Sourcefn acquire<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LockError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn acquire<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, LockError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Try to acquire the lock for key with the given ttl.
Returns Some(token) on success, None if the lock is held by someone else.
Sourcefn release<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 str,
token: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<(), LockError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn release<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 str,
token: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<(), LockError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Release the lock for key, but only if token still matches the holder.
Provided Methods§
Sourcefn renew<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_key: &'life1 str,
_token: &'life2 str,
_ttl: Duration,
) -> Pin<Box<dyn Future<Output = Result<(), LockError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn renew<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_key: &'life1 str,
_token: &'life2 str,
_ttl: Duration,
) -> Pin<Box<dyn Future<Output = Result<(), LockError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Extend the lock’s TTL to ttl, but only if token still matches the holder.
默认实现返回错误而非静默成功:不支持续租的后端必须让调用方知道 「我以为续上了,其实没有」是危险的(锁会在预期之前过期)。
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".