Reference / Locks API for Rust
Locks API for Rust
Every public type, trait and function of the Locks engine in tinystore::locks and tinystore::aio::locks, generated from its source. The Locks guide explains how to use them, and the Bun and Node, Python and Go pages list the same API.
Store
impl StoreStore.lock
pub fn lock(&self, name: &str) -> LockBuilderKeys held one holder at a time, across every process of the store:
hold waits for a key, runs a function and lets the key go.
Store.semaphore
pub fn semaphore(&self, name: &str) -> SemaphoreBuilderKeys held by up to limit holders at a time.
aio::Store
impl aio::Storeaio::Store.lock
pub fn lock(&self, name: &str) -> LockBuilderKeys held one holder at a time, across every process of the store, as
crate::Store::lock: a hold is awaited.
aio::Store.semaphore
pub fn semaphore(&self, name: &str) -> SemaphoreBuilderKeys held by up to limit holders at a time, as
crate::Store::semaphore.
KeyState
pub struct KeyState {
pub up: bool,
pub down_since: Option<SystemTime>,
pub last_failure: Option<SystemTime>,
pub waiting: u32,
pub holding: u32,
pub failures: u32,
}How a semaphore's key stands now: whether what it guards is up, and how many wait for it and hold it.
Fence
pub struct Fence(_)A number that grows with every lease a store gives, so that whoever keeps the largest it has seen can refuse a late holder: the store's epoch in the high 64 bits, which grows each time the locks of a store open, and a count of its leases since in the low 64.
Fence.get
pub fn get(self) -> u128The fence as one integer, compared as it is.
LockBuilder
pub struct LockBuilder { /* private fields */ }A lock being opened.
LockBuilder.max_hold
pub fn max_hold(self, bound: Duration) -> SelfHow long one lease may last from its grant: past it the key is given to the next in line and the holder told it lost it. Off unless given.
LockBuilder.open
pub fn open(self) -> Result<Lock>SemaphoreBuilder
pub struct SemaphoreBuilder { /* private fields */ }A semaphore being opened.
SemaphoreBuilder.limit
pub fn limit(self, limit: u32) -> SelfHow many may hold a key at once, from 1 to 1000. A name has one limit
in a program: an open with another limit than the name's first open
in the program gave is Invalid. Another program's holds, a client's
over a connection, are each admitted by their own limit.
SemaphoreBuilder.background
pub fn background(self, places: u32) -> SelfHow many of a key's places background holds may take at once, from 1 to the limit, so that urgent holds always find the rest; background holds may take every place unless given. A name keeps one in a program, as it keeps its limit.
SemaphoreBuilder.breaker
pub fn breaker(self, failures: u32, pause: Duration) -> SelfPuts a key down for pause once failures holds of it failed in a
row: every ask of it, waiting or new, is then refused at once
Unavailable. When the pause is over, one ask goes through. Its
success puts the key up again, and its failure pauses it again.
A hold failed when its work answered an error, unless its permit said
Permit::healthy, or when it ran past its timeout or max_hold. A
success resets the count. The breaker lives in the memory of the
process that holds the store, beside the lines: every program of the
store sees one answer, and a restart of that process starts every key
up. A name keeps one breaker in a program, as it keeps its limit.
SemaphoreBuilder.max_hold
pub fn max_hold(self, bound: Duration) -> SelfSemaphoreBuilder.open
pub fn open(self) -> Result<Semaphore>Lock
pub struct Lock { /* private fields */ }A lock's keys, each held by one holder at a time.
Lock.hold
pub fn hold<T, E: From<Error>>(
&self,
key: impl Key,
work: impl FnOnce(&Lease<'_>) -> Result<T, E>,
) -> Result<T, E>Waits for key, ten seconds at most, holds it while work runs and
lets it go, answering what work answered. A key still held after the
wait is in use; a lease lost while work ran is lost, even when
work succeeded.
Lock.try_hold
pub fn try_hold<T, E: From<Error>>(
&self,
key: impl Key,
work: impl FnOnce(&Lease<'_>) -> Result<T, E>,
) -> Result<Option<T>, E>Holds key while work runs if nobody holds it, and answers None
without waiting if somebody does.
Lock.key
pub fn key(&self, key: impl Key) -> LockKey<'_>A key, for a hold that says how long it waits.
Semaphore
pub struct Semaphore { /* private fields */ }A semaphore's keys, each held by up to its limit at a time.
Semaphore.hold
pub fn hold<T, E: From<Error>>(
&self,
key: impl Key,
work: impl FnOnce(&Permit<'_>) -> Result<T, E>,
) -> Result<T, E>Semaphore.try_hold
pub fn try_hold<T, E: From<Error>>(
&self,
key: impl Key,
work: impl FnOnce(&Permit<'_>) -> Result<T, E>,
) -> Result<Option<T>, E>Semaphore.key
pub fn key(&self, key: impl Key) -> SemaphoreKey<'_>A key, for a hold that says how long it waits, whether it waits in the background and how long it may keep its place.
Semaphore.state
pub fn state(&self, key: impl Key) -> Result<KeyState>How key stands now: up or down and since when, its last failed
hold, and how many wait for it and hold it. Up always without a
breaker.
LockKey
pub struct LockKey<'a> { /* private fields */ }A key of a lock, for a hold that says how long it waits.
LockKey.wait
pub fn wait(self, wait: Duration) -> SelfHow long the hold waits in line; ten seconds unless said.
LockKey.hold
pub fn hold<T, E: From<Error>>(self, work: impl FnOnce(&Lease<'_>) -> Result<T, E>) -> Result<T, E>LockKey.try_hold
pub fn try_hold<T, E: From<Error>>(
self,
work: impl FnOnce(&Lease<'_>) -> Result<T, E>,
) -> Result<Option<T>, E>SemaphoreKey
pub struct SemaphoreKey<'a> { /* private fields */ }A key of a semaphore, for a hold that says how long it waits, at which priority, and how long it may keep its place.
SemaphoreKey.wait
pub fn wait(self, wait: Duration) -> SelfSemaphoreKey.background
pub fn background(self) -> SelfWaits behind every urgent hold of the key, and takes no more of its
places than the semaphore's background. Nothing promises it a place:
while urgent holds keep coming and take the room, it waits, wait at
most.
SemaphoreKey.timeout
pub fn timeout(self, timeout: Duration) -> SelfHow long the hold may keep its place, from its grant: past it the
place goes to the next in line and the hold fails lost, as at the
semaphore's max_hold, the shorter of the two counting.
SemaphoreKey.hold
pub fn hold<T, E: From<Error>>(self, work: impl FnOnce(&Permit<'_>) -> Result<T, E>) -> Result<T, E>SemaphoreKey.try_hold
pub fn try_hold<T, E: From<Error>>(
self,
work: impl FnOnce(&Permit<'_>) -> Result<T, E>,
) -> Result<Option<T>, E>Lease
pub struct Lease<'a> { /* private fields */ }A holding of a lock's key, given to the function that holds it.
Lease.fence
pub fn fence(&self) -> FenceThe lease's fence: hand it to whatever must refuse a late holder.
Lease.lost
pub fn lost(&self) -> boolWhether the lease ended while its function runs: revoked at its
max_hold, or the store closed. What the function does from then on
may run beside another holder.
Lease.waited
pub fn waited(&self) -> DurationHow long the hold waited for the key, from its call to the grant.
Permit
pub struct Permit<'a> { /* private fields */ }A holding of a semaphore's key: one of its limit.
Permit.lost
pub fn lost(&self) -> boolPermit.healthy
pub fn healthy(&self)Says the guarded service is healthy, so that the semaphore's breaker does not count the hold a failure though its work then answers an error: a 404 that is the caller's, not the service's.
Permit.waited
pub fn waited(&self) -> DurationHow long the hold waited for its place, from its call to the grant.
aio::locks::LockBuilder
pub struct LockBuilder { /* private fields */ }A lock being opened, as crate::locks::LockBuilder.
aio::locks::LockBuilder.max_hold
pub fn max_hold(self, bound: Duration) -> SelfHow long one lease may last from its grant: past it the key is given
to the next in line and the hold answers lost. Off unless given.
aio::locks::LockBuilder.open
pub fn open(self) -> Result<Lock>Opens the lock: a plain call, made once at start. The first lock or semaphore of a store writes the locks' epoch to kv.db.
aio::locks::SemaphoreBuilder
pub struct SemaphoreBuilder { /* private fields */ }A semaphore being opened, as crate::locks::SemaphoreBuilder.
aio::locks::SemaphoreBuilder.limit
pub fn limit(self, limit: u32) -> SelfHow many may hold a key at once, from 1 to 1000.
aio::locks::SemaphoreBuilder.background
pub fn background(self, places: u32) -> SelfHow many of a key's places background holds may take at once, as
crate::locks::SemaphoreBuilder::background.
aio::locks::SemaphoreBuilder.breaker
pub fn breaker(self, failures: u32, pause: Duration) -> SelfPuts a key down for pause once failures holds of it failed in a
row, as crate::locks::SemaphoreBuilder::breaker: a hold whose
future answered an error failed, and one whose future was dropped
counts neither way.
aio::locks::SemaphoreBuilder.max_hold
pub fn max_hold(self, bound: Duration) -> Selfaio::locks::SemaphoreBuilder.open
pub fn open(self) -> Result<Semaphore>Opens the semaphore: a plain call, made once at start.
aio::locks::Lock
pub struct Lock { /* private fields */ }A lock's keys, each held by one holder at a time, as
crate::locks::Lock.
aio::locks::Lock.hold
pub fn hold<'w, T, E, F>(
&self,
key: impl Key,
work: impl FnOnce(Lease) -> F + Send + 'w,
) -> impl Future<Output = Result<T, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,Waits for key, ten seconds at most, holds it while the future work
makes runs and lets it go, answering what that future answered. A key
still held after the wait is in use; a lease lost meanwhile is
lost, even when work succeeded.
The future is as 'static as work and what it borrows, so a hold
whose work owns what it uses is spawned.
aio::locks::Lock.try_hold
pub fn try_hold<'w, T, E, F>(
&self,
key: impl Key,
work: impl FnOnce(Lease) -> F + Send + 'w,
) -> impl Future<Output = Result<Option<T>, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,Holds key while work runs if nobody holds it, and answers None
without waiting if somebody does.
aio::locks::Lock.key
pub fn key(&self, key: impl Key) -> LockKey<'_>A key, for a hold that says how long it waits.
aio::locks::Lock.blocking
pub fn blocking(&self) -> &LockThe same lock, for its blocking calls.
aio::locks::Semaphore
pub struct Semaphore { /* private fields */ }A semaphore's keys, each held by up to its limit at a time, as
crate::locks::Semaphore.
aio::locks::Semaphore.hold
pub fn hold<'w, T, E, F>(
&self,
key: impl Key,
work: impl FnOnce(Permit) -> F + Send + 'w,
) -> impl Future<Output = Result<T, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,As Lock::hold, one of the key's limit holders.
aio::locks::Semaphore.try_hold
pub fn try_hold<'w, T, E, F>(
&self,
key: impl Key,
work: impl FnOnce(Permit) -> F + Send + 'w,
) -> impl Future<Output = Result<Option<T>, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,As Lock::try_hold: None at once while limit hold the key.
aio::locks::Semaphore.key
pub fn key(&self, key: impl Key) -> SemaphoreKey<'_>aio::locks::Semaphore.state
pub fn state(&self, key: impl Key) -> Result<KeyState>How key stands now, as crate::locks::Semaphore::state: a plain
call, since the store's process keeps it in memory.
aio::locks::Semaphore.blocking
pub fn blocking(&self) -> &SemaphoreThe same semaphore, for its blocking calls.
aio::locks::LockKey
pub struct LockKey<'a> { /* private fields */ }A key of a lock, for a hold that says how long it waits.
aio::locks::LockKey.wait
pub fn wait(self, wait: Duration) -> SelfHow long the hold waits in line; ten seconds unless said. The wait ends on the locks' timer, with no thread parked.
aio::locks::LockKey.hold
pub fn hold<'w, T, E, F>(
self,
work: impl FnOnce(Lease) -> F + Send + 'w,
) -> impl Future<Output = Result<T, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,As Lock::hold.
aio::locks::LockKey.try_hold
pub fn try_hold<'w, T, E, F>(
self,
work: impl FnOnce(Lease) -> F + Send + 'w,
) -> impl Future<Output = Result<Option<T>, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,As Lock::try_hold.
aio::locks::SemaphoreKey
pub struct SemaphoreKey<'a> { /* private fields */ }A key of a semaphore, for a hold that says how long it waits.
aio::locks::SemaphoreKey.wait
pub fn wait(self, wait: Duration) -> SelfHow long the hold waits in line; ten seconds unless said.
aio::locks::SemaphoreKey.background
pub fn background(self) -> SelfWaits behind every urgent hold of the key, as
crate::locks::SemaphoreKey::background: nothing promises it a place
while urgent holds keep coming.
aio::locks::SemaphoreKey.timeout
pub fn timeout(self, timeout: Duration) -> SelfHow long the hold may keep its place from its grant, as
crate::locks::SemaphoreKey::timeout: past it the hold answers
lost.
aio::locks::SemaphoreKey.hold
pub fn hold<'w, T, E, F>(
self,
work: impl FnOnce(Permit) -> F + Send + 'w,
) -> impl Future<Output = Result<T, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,As Semaphore::hold.
aio::locks::SemaphoreKey.try_hold
pub fn try_hold<'w, T, E, F>(
self,
work: impl FnOnce(Permit) -> F + Send + 'w,
) -> impl Future<Output = Result<Option<T>, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,As Semaphore::try_hold.
aio::locks::Lease
pub struct Lease { /* private fields */ }A holding of a lock's key, given to the function that holds it, and owned by it: the future it makes may keep it across its awaits.
aio::locks::Lease.fence
pub fn fence(&self) -> FenceThe lease's fence: hand it to whatever must refuse a late holder.
aio::locks::Lease.lost
pub fn lost(&self) -> boolWhether the lease ended: revoked at its max_hold, the store closed,
or, for a lease kept past its hold, the hold over. What is done with
it from then on may run beside another holder.
aio::locks::Lease.waited
pub fn waited(&self) -> DurationHow long the hold waited for the key, from its first poll to the grant.
aio::locks::Permit
pub struct Permit { /* private fields */ }A holding of a semaphore's key, one of its limit, owned by the function that holds it.
aio::locks::Permit.lost
pub fn lost(&self) -> boolAs Lease::lost: true too once its hold is over.
aio::locks::Permit.healthy
pub fn healthy(&self)As crate::locks::Permit::healthy: the breaker does not count the
hold a failure though its future then answers an error.
aio::locks::Permit.waited
pub fn waited(&self) -> DurationAs Lease::waited.