Struct tokio_etcd_lock::Lease
source · pub struct Lease { /* private fields */ }
Implementations§
source§impl Lease
impl Lease
pub async fn lock<'l, 'n>( &'l mut self, client: &mut Client, name: &'n str ) -> Result<LockGuard<'l, 'n>, Error>
sourcepub async fn try_lock<'l, 'n>(
&'l mut self,
client: &mut Client,
name: &'n str
) -> Result<LockGuard<'l, 'n>, Error>
pub async fn try_lock<'l, 'n>( &'l mut self, client: &mut Client, name: &'n str ) -> Result<LockGuard<'l, 'n>, Error>
Anologous to lock
, but if the lock is taken, return Error::Taken
instead of waiting for the lock to become available.
sourcepub async fn with_lock<F, Fut, T>(
&mut self,
client: &mut Client,
name: &str,
f: F
) -> Result<T, Error>where
F: FnOnce() -> Fut,
Fut: Future<Output = T>,
pub async fn with_lock<F, Fut, T>( &mut self, client: &mut Client, name: &str, f: F ) -> Result<T, Error>where F: FnOnce() -> Fut, Fut: Future<Output = T>,
Run and await future-returning function f
, but only in the context of
an acquired lock.
Attempt to acquire lock with name
, waiting for it to become available
if needed, and run f
after it is successfuly acquired. After f
is finished and the future it returned is awaited, the lock is released
automatically.
Panic handling
This function does not attempt to catch panics from f
, and does not
guarantee that the lock is released in such cases. If the program
exits abruptly, the lock will be not be available to other clients until
this lease’s TTL runs out.
Example
lease.with_lock(&mut client, "lock-key", || async {
println!("Holding mutually exclusive access to the lock!");
}).await?;
// at this point, the lock has been released and is available for other clients
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Lease
impl Send for Lease
impl Sync for Lease
impl Unpin for Lease
impl !UnwindSafe for Lease
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
Mutably borrows from an owned value. Read more
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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T
in a tonic::Request