LockManager

Struct LockManager 

Source
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

Source

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"

Source

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.

Source

pub fn get_unique_lock_id(&self) -> Result<Vec<u8>>

Get 20 random bytes from the pseudorandom interface.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub async fn extend( &self, lock: &Lock, ttl: Duration, ) -> Result<Lock, LockError>

Extend the given lock by given time in milliseconds

Source

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

Source§

fn clone(&self) -> LockManager

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LockManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,