Skip to main content

Lock

Struct Lock 

Source
pub struct Lock { /* private fields */ }
Expand description

A named lock, not yet held.

Built by LockManager::lock. Nothing is taken until acquire or run.

Implementations§

Source§

impl Lock

Source

pub fn wait_for(self, wait: Duration) -> Self

Wait up to wait for the lock instead of giving up immediately.

Polls. That is deliberate: the alternative is a pub/sub channel per lock name, which is a lot of moving parts to shave a hundred milliseconds off a path that is by definition contended and by definition rare.

Use it for “this must happen, just not twice at once”. Leave it off for “if somebody else is already doing this, I have nothing to do” — which is the scheduler’s case, and why without_overlapping does not wait.

Source

pub async fn acquire(&self) -> Result<Option<LockGuard>>

Take the lock, or return None if somebody else holds it.

Source

pub async fn run<T, F>(&self, work: F) -> Result<Option<T>>
where F: Future<Output = Result<T>>,

Run work while holding the lock, or return None without running it.

The lock is released whether work succeeds or fails — a task that errors must not hold its lock until the TTL expires, or one bad night skips the next hour of runs too.

let lock = locks.lock("import", Duration::from_secs(60));

match lock.run(async { Ok::<_, rainier_support::Error>(42) }).await? {
    Some(value) => assert_eq!(value, 42),
    None => unreachable!("nobody else holds it"),
}
Source

pub async fn force_release(&self) -> Result<bool>

Take the lock over from whoever holds it.

An escape hatch for an operator, not for code. The holder does not find out, and if it is still running you now have two.

Source

pub async fn is_held(&self) -> Result<bool>

Whether anybody holds it.

Racy by nature — true the moment you ask and false the moment after — so it is for a status page, not for deciding whether to acquire.

Source

pub fn key(&self) -> &str

The cache key, for diagnostics.

Trait Implementations§

Source§

impl Debug for Lock

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Lock

§

impl !UnwindSafe for Lock

§

impl Freeze for Lock

§

impl Send for Lock

§

impl Sync for Lock

§

impl Unpin for Lock

§

impl UnsafeUnpin for Lock

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, 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<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