pub struct LockManager { /* private fields */ }Expand description
The lock manager.
Implements the necessary functionality to acquire and release locks and handles the Redis connections.
Implementations§
Source§impl LockManager
impl LockManager
Sourcepub fn new<T: IntoConnectionInfo>(uris: Vec<T>) -> LockManager
pub fn new<T: IntoConnectionInfo>(uris: Vec<T>) -> LockManager
Create a new lock manager instance, defined by the given Redis connection uris.
Sample URI: "redis://127.0.0.1:6379"
Sourcepub fn from_clients(clients: Vec<Client>) -> LockManager
pub fn from_clients(clients: Vec<Client>) -> LockManager
Create a new lock manager instance, defined by the given Redis clients. Quorum is defined to be N/2+1, with N being the number of given Redis instances.
Sourcepub fn get_unique_lock_id(&self) -> Result<Vec<u8>>
pub fn get_unique_lock_id(&self) -> Result<Vec<u8>>
Get 20 random bytes from the pseudorandom interface.
Sourcepub fn set_retry(&mut self, count: u32, delay: Duration)
pub fn set_retry(&mut self, count: u32, delay: Duration)
Set retry count and retry delay.
Retries will be delayed by a random amount of time between 0 and retry_delay.
Retry count defaults to 3.
Retry delay defaults to 200.
Sourcepub async fn unlock(&self, lock: &Lock)
pub async fn unlock(&self, lock: &Lock)
Unlock the given lock.
Unlock is best effort. It will simply try to contact all instances and remove the key.
Sourcepub async fn lock(
&self,
resource: impl ToLockResource<'_>,
ttl: Duration,
) -> Result<Lock, LockError>
pub async fn lock( &self, resource: impl ToLockResource<'_>, ttl: Duration, ) -> Result<Lock, LockError>
Acquire the lock for the given resource and the requested TTL.
If it succeeds, a Lock instance is returned,
including the value and the validity time
If it fails. None is returned.
A user should retry after a short wait time.
May return LockError::TtlTooLarge if ttl is too large.
Sourcepub async fn acquire(
&self,
resource: impl ToLockResource<'_>,
ttl: Duration,
) -> Result<LockGuard, LockError>
pub async fn acquire( &self, resource: impl ToLockResource<'_>, ttl: Duration, ) -> Result<LockGuard, LockError>
Loops until the lock is acquired.
The lock is placed in a guard that will unlock the lock when the guard is dropped.
May return LockError::TtlTooLarge if ttl is too large.
Sourcepub async fn acquire_no_guard(
&self,
resource: impl ToLockResource<'_>,
ttl: Duration,
) -> Result<Lock, LockError>
pub async fn acquire_no_guard( &self, resource: impl ToLockResource<'_>, ttl: Duration, ) -> Result<Lock, LockError>
Loops until the lock is acquired.
Either lock’s value must expire after the ttl has elapsed,
or LockManager::unlock must be called to allow other clients to lock the same resource.
May return LockError::TtlTooLarge if ttl is too large.
Sourcepub async fn extend(
&self,
lock: &Lock,
ttl: Duration,
) -> Result<Lock, LockError>
pub async fn extend( &self, lock: &Lock, ttl: Duration, ) -> Result<Lock, LockError>
Extend the given lock by given time in milliseconds
Sourcepub async fn is_freed(&self, lock: &Lock) -> Result<bool, LockError>
pub async fn is_freed(&self, lock: &Lock) -> Result<bool, LockError>
Checks if the given lock has been freed (i.e., is no longer held).
This method queries Redis to determine if the key associated with the lock is still present and matches the value of this lock. If the key is missing or the value does not match, the lock is considered freed.
§Returns
Ok(true) if the lock is considered freed (either because the key does not exist
or the value does not match), otherwise Ok(false). Returns an error if a Redis
connection or query fails.
Trait Implementations§
Source§impl Clone for LockManager
impl Clone for LockManager
Source§fn clone(&self) -> LockManager
fn clone(&self) -> LockManager
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more