Struct ManagedLock

Source
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

Source

pub fn lease_id(&self) -> i64

Source

pub async fn txn(&self, operations: impl Into<Vec<TxnOp>>) -> TxnResponse

Execute an etcd transaction if and only if the lock is still alive.

Source

pub fn get_revoke_notify(&self) -> ManagedLockRevokeNotify

Get a revoke notify handle to be notified when the lock is revoked.

Source

pub async fn is_alive(&self) -> bool

Check if the lock is still alive.

Source

pub fn get_key(&self) -> Vec<u8>

Get the underlying unique lock key.

Source

pub async fn scope<T, Fut>(&self, fut: Fut) -> Result<T, LockError>
where T: Send + 'static, Fut: Future<Output = T> + Send + 'static,

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;
});
Source

pub async fn scope_with<'a, T, F, Fut>( &'a self, func: F, ) -> Result<T, LockError>
where T: Send + 'a, F: FnOnce(ManagedLockGuard<'a>) -> Fut, Fut: Future<Output = T> + Send + 'a,

Similar to ManagedLock::scope but accept a closure to compute the future to execute against the lock.

Source

pub fn get_managed_lease_weak_ref(&self) -> ManagedLeaseWeak

Get a weak reference to the managed lease.

Source

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

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for ManagedLock

Source§

fn drop(&mut self)

Executes the destructor for this type. 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more