Reference / Locks API for Bun and Node
Locks API for Bun and Node
Every public class, function and type of the Locks engine in @tinyshed/tinystore, generated from its source. The Locks guide explains how to use them, and the Python, Go and Rust pages list the same API.
LockOptions
interface LockOptions {
/** how long a lease lasts from its last renewal: 10 s unless said, 1 s to 1 hour */
ttl?: Duration | undefined
/** how long a lease lasts from its grant, however it is renewed: past it the next in line holds the key */
maxHold?: Duration | undefined
}SemaphoreOptions
interface SemaphoreOptions extends LockOptions {
/** how many may hold a key at once, 1 to 1000 */
limit: number
/** how many of a key's places background holds may take at once, 1 to limit: every place unless said */
background?: number | undefined
/** puts a key down for `pause` once `failures` holds of it failed in a row: every ask is then UnavailableError at once */
breaker?: Breaker | undefined
}Breaker
interface Breaker {
/** failed holds in a row, 1 or more */
failures: number
pause: Duration
}A semaphore's breaker. A hold failed when its function threw, unless its
permit said healthy(), or when it ran past its timeout or maxHold; a
success resets the count. Once the pause is over, one hold goes through:
its success puts the key up, its failure pauses it again. The store's
process keeps it in memory, so a restart of that process puts every key up.
KeyState
interface KeyState {
/** false from the failure that put the key down until a hold after the pause succeeds; true without a breaker */
up: boolean
downSince?: Date | undefined
/** the last failed hold's end, kept until a success after it */
lastFailure?: Date | undefined
waiting: number
holding: number
/**
* the failed holds in a row the breaker counts now, toward its `failures`:
* reset by a success, and by a pause with no failure; while the key is
* down, those that put it down; 0 without a breaker
*/
failures: number
}How a semaphore's key stands now: semaphore.state(key).
HoldOptions
interface HoldOptions {
/** how long the hold waits in line: 10 s unless said, then InUseError */
wait?: Duration | undefined
}Priority
type Priority = 'urgent' | 'background'Which line a semaphore's hold waits in: urgent holds are given a place
before background ones, and a background hold waiting never keeps an urgent
one from a free place. A background hold is promised no place: it waits
while urgent holds keep coming, wait at most.
PermitOptions
interface PermitOptions {
/** urgent unless said */
priority?: Priority | undefined
/** how long the hold may keep its place, from its grant: past it the next in line holds it and the hold is LostError */
timeout?: Duration | undefined
}SemaphoreHoldOptions
interface SemaphoreHoldOptions extends HoldOptions, PermitOptions {
}Tried
type Tried<T> = {
acquired: true
value: T
} | {
acquired: false
}What tryHold gives: the function's value, or that the key was held.
Lease
interface Lease {
/** grows with every lease the store gives: hand it to whatever must refuse a late holder */
readonly fence: bigint
/** aborts when the lease is lost: run out, revoked at maxHold, or its store closed */
readonly signal: AbortSignal
/** how long the hold waited for the key, from its call to the grant, the round trip included */
readonly waited: DurationText
}A holding of a lock's key, given to the function that holds it.
Permit
interface Permit {
readonly signal: AbortSignal
/** how long the hold waited for its place, from its call to the grant, the round trip included */
readonly waited: DurationText
/** says the guarded service is healthy, so that the breaker does not count the hold a failure though fn then throws: a 404 that is the caller's */
healthy(): void
}A holding of a semaphore's key: one of its limit.
Lock
class Lock {
readonly name: string
}Keys held one holder at a time: open them with store.lock(name).
Lock.hold
hold<T>(key: Key, fn: (lease: Lease) => T | Promise<T>, options?: HoldOptions): Promise<T>Waits for key, holds it while fn runs and lets it go, giving what fn gives. A key still held after the wait is InUseError; a lease lost while fn ran is LostError, though fn succeeded.
await invoices.hold(invoice.id, lease => charge(invoice, { fence: lease.fence, signal: lease.signal }))Lock.tryHold
tryHold<T>(key: Key, fn: (lease: Lease) => T | Promise<T>): Promise<Tried<T>>Holds key while fn runs if nobody holds it, and says it was held without waiting if somebody does.
Semaphore
class Semaphore {
readonly name: string
readonly limit: number
}Keys held by up to their limit at a time: open them with store.semaphore(name, { limit }).
Semaphore.hold
hold<T>(key: Key, fn: (permit: Permit) => T | Promise<T>, options?: SemaphoreHoldOptions): Promise<T>Waits for one of key's places, holds it while fn runs and lets it go,
as a lock's hold does; in the background, it waits behind every urgent
hold and takes no more than the semaphore's background places:
await cabinet.hold('requests', permit => sync(permit.signal), { priority: 'background', timeout: '15s' })Semaphore.tryHold
tryHold<T>(key: Key, fn: (permit: Permit) => T | Promise<T>, options?: PermitOptions): Promise<Tried<T>>Semaphore.state
state(key: Key): Promise<KeyState>How key stands now: whether what it guards is up, since when it is down, its last failed hold, and how many wait for it and hold it.
const { up, downSince } = await cabinet.state('requests')