pub struct ManagedLock {
pub created_at_revision: Revision,
/* private fields */
}
Expand description
A Lock instance with automatic lease refresh and lock revocation when dropped.
Fields§
§created_at_revision: Revision
Implementations§
Source§impl ManagedLock
impl ManagedLock
pub fn lease_id(&self) -> i64
Sourcepub async fn txn(&self, operations: impl Into<Vec<TxnOp>>) -> TxnResponse
pub async fn txn(&self, operations: impl Into<Vec<TxnOp>>) -> TxnResponse
Execute an etcd transaction if and only if the lock is still alive.
Sourcepub fn get_revoke_notify(&self) -> ManagedLockRevokeNotify
pub fn get_revoke_notify(&self) -> ManagedLockRevokeNotify
Get a revoke notify handle to be notified when the lock is revoked.
Sourcepub async fn scope<T, Fut>(&self, fut: Fut) -> Result<T, LockError>
pub async fn scope<T, Fut>(&self, fut: Fut) -> Result<T, LockError>
This function make sure the future is executed within a valid managed lock lifetime.
If the lock is revoked, it will cancel the future and return a LockError::LockRevoked.
Make sure the future returned by the closure is cancel safe.
Examples
use etcd_client::Client;
use rust_etcd_utils::{lease::ManagedLeaseFactory, lock::spawn_lock_manager, ManagedLock};
let etcd = Client::connect(["http://localhost:2379"], None).await.expect("failed to connect to etcd");
let (lock_man_handle, lock_man) = spawn_lock_manager(etcd.clone());
// Do something with the lock manager
let managed_lock: ManagedLock = lock_man.try_lock("test").await.expect("failed to lock");
managed_lock.scope(async move {
// execute only if my lock is valid
access_protected_ressource().await;
});
Sourcepub async fn scope_with<'a, T, F, Fut>(
&'a self,
func: F,
) -> Result<T, LockError>
pub async fn scope_with<'a, T, F, Fut>( &'a self, func: F, ) -> Result<T, LockError>
Similar to ManagedLock::scope
but accept a closure to compute the future to execute against the lock.
Sourcepub fn get_managed_lease_weak_ref(&self) -> ManagedLeaseWeak
pub fn get_managed_lease_weak_ref(&self) -> ManagedLeaseWeak
Get a weak reference to the managed lease.
Sourcepub async fn into_revoked_fut(self)
pub async fn into_revoked_fut(self)
Convert the managed lock into a signal that will be resolved when the lock is revoked.
Trait Implementations§
Source§impl Debug for ManagedLock
impl Debug for ManagedLock
Auto Trait Implementations§
impl !Freeze for ManagedLock
impl !RefUnwindSafe for ManagedLock
impl Send for ManagedLock
impl Sync for ManagedLock
impl Unpin for ManagedLock
impl !UnwindSafe for ManagedLock
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