pub struct MultiResourceLock { /* private fields */ }
Expand description
A distributed mutual exclusion lock backed by Redis.
Supports exclusion based on multiple resources and partial overlaps.
This is much less efficient than lock_across
. Ideally you should architect your
application so you never need MultiResourceLock
.
E.g. a lock on resources ["a", "b"]
will block a lock on ["a"]
or ["b", "c"]
.
Implementations§
Source§impl MultiResourceLock
impl MultiResourceLock
Sourcepub fn new(client: Client) -> RedisResult<Self>
pub fn new(client: Client) -> RedisResult<Self>
Sourcepub async fn acquire_default(
&mut self,
resources: &[String],
) -> RedisResult<Option<String>>
pub async fn acquire_default( &mut self, resources: &[String], ) -> RedisResult<Option<String>>
Calls MultiResourceLock::acquire
with DEFAULT_EXPIRATION
, DEFAULT_TIMEOUT
and DEFAULT_SLEEP
.
§Errors
When MultiResourceLock::acquire
errors.
Sourcepub async fn acquire(
&mut self,
resources: &[String],
expiration: Duration,
timeout: Duration,
sleep: Duration,
) -> RedisResult<Option<String>>
pub async fn acquire( &mut self, resources: &[String], expiration: Duration, timeout: Duration, sleep: Duration, ) -> RedisResult<Option<String>>
Attempts to acquire the lock blocking until the lock can be acquired.
Blocks up to timeout
duration making attempts every sleep
duration.
Returns None
when it times out.
§Errors
When MultiResourceLock::try_acquire
errors.
Sourcepub async fn try_acquire_default(
&mut self,
resources: &[String],
) -> RedisResult<Option<String>>
pub async fn try_acquire_default( &mut self, resources: &[String], ) -> RedisResult<Option<String>>
Calls MultiResourceLock::try_acquire
with DEFAULT_EXPIRATION
.
§Errors
When MultiResourceLock::try_acquire
errors.
Sourcepub async fn try_acquire(
&mut self,
resources: &[String],
expiration: Duration,
) -> RedisResult<Option<String>>
pub async fn try_acquire( &mut self, resources: &[String], expiration: Duration, ) -> RedisResult<Option<String>>
Attempts to acquire the lock returning immediately if it cannot be immediately acquired.
§Errors
- When the
acquire_lock
function is missing from the Redis instance.
Sourcepub async fn release(&mut self, lock_id: &str) -> RedisResult<usize>
pub async fn release(&mut self, lock_id: &str) -> RedisResult<usize>
Releases a held lock.
§Errors
- When the
release_lock
function is missing from the Redis instance. - When
lock_id
does not refer to a held lock.
Sourcepub async fn map<F>(
&mut self,
resources: &[String],
expiration: Duration,
timeout: Duration,
sleep: Duration,
f: F,
) -> Result<F::Output, MapError>
pub async fn map<F>( &mut self, resources: &[String], expiration: Duration, timeout: Duration, sleep: Duration, f: F, ) -> Result<F::Output, MapError>
Since we cannot safely drop a guard in an async context, we need to provide a way to release the lock in case of an error.
This is the suggested approach, it is less ergonomic but it is safe.
Sourcepub async fn map_default<F>(
&mut self,
resources: &[String],
f: F,
) -> Result<F::Output, MapError>
pub async fn map_default<F>( &mut self, resources: &[String], f: F, ) -> Result<F::Output, MapError>
Calls MultiResourceLock::map
with DEFAULT_EXPIRATION
, DEFAULT_TIMEOUT
and DEFAULT_SLEEP
.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for MultiResourceLock
impl RefUnwindSafe for MultiResourceLock
impl Send for MultiResourceLock
impl Sync for MultiResourceLock
impl Unpin for MultiResourceLock
impl UnwindSafe for MultiResourceLock
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more