Locks / Locks
Locks
The Locks engine gives one holder of a key at a time, across every process and connection of a store: one summary per chat, one charge per invoice. Waiters are served in order, a lease over a connection is renewed while its holder works, and every lease carries a fence, a number that only grows, so that a holder that stalled past its lease can be refused.
const summaries = store.lock('summaries', { ttl: '30s' })
await summaries.hold(chatId, async lease => {
await writeSummary(chatId, lease.fence)
})
const tried = await summaries.tryHold(chatId, lease => writeSummary(chatId, lease.fence))
if (!tried.acquired) return // somebody holds itsummaries = store.lock("summaries", ttl="30s")
async with summaries.hold(chat_id) as lease:
await write_summary(chat_id, lease.fence)
async with summaries.try_hold(chat_id) as lease:
if lease is None:
return # somebody holds it
await write_summary(chat_id, lease.fence)summaries, err := locks.OpenLock(store, "summaries", locks.TTL(30*time.Second))
err = summaries.Hold(ctx, chatID, func(ctx context.Context, lease *locks.Lease) error {
return writeSummary(ctx, chatID, lease.Fence)
})
acquired, err := summaries.TryHold(ctx, chatID, func(ctx context.Context, lease *locks.Lease) error {
return writeSummary(ctx, chatID, lease.Fence)
})What it guarantees
- A key has one holder at a time. Waiters of the same priority get it in the order they arrived (contract LOCK-01).
- Every lease's fence is larger than every earlier lease's, across restarts (contract LOCK-02).
- A lease over a connection lasts its
ttlfrom its last renewal; one that ran out is never renewed (contract LOCK-05). - A panic, a cancel or the store's close lets the key go and answers every waiter (contract LOCK-06).
- Not promised: locks live in the memory of the process that holds the store, and do not coordinate two stores (NOT-03).
In these guides
| Page | Covers |
|---|---|
| Holding and waiting | hold, tryHold, wait, maxHold |
| Leases over a connection | ttl, renewal, a lost lease |
| Fences | refusing a late write in SQL or another system |
| Semaphores | up to N holders of a key, urgent or background |
New to Locks? Live updates in the Handbook writes one summary per chat at a time, with a fence in SQL.
Async Rust
With the async feature, a Rust program opens the same locks and semaphores
from tinystore::aio::Store, and a hold is awaited.
Rust with async explains where the work
runs and what dropping a future does.
use tinystore::aio::Store;
let store = Store::open("data", Options::default())?;
let summaries = store.lock("summaries").max_hold(Duration::from_secs(120)).open()?;
// blocking: summaries.hold(chat_id, |lease| write_summary(chat_id, lease.fence()))?
summaries.hold(chat_id, |lease| async move { write_summary(chat_id, lease.fence()).await }).await?;
let tried = summaries.try_hold(chat_id, |lease| async move { write_summary(chat_id, lease.fence()).await }).await?;
summaries
.key(chat_id)
.wait(Duration::from_secs(30))
.hold(|lease| async move { write_summary(chat_id, lease.fence()).await })
.await?;
let uploads = store.semaphore("uploads").limit(3).open()?;
uploads.hold(user_id, |_permit| async move { upload(user_id).await }).await?;- The function you pass gets the lease by value and returns a future, which runs while the key is held. The key goes to the next holder when that future ends, fails or is dropped.
- A hold waits, fails and is refused as in blocking Rust: past
waitit fails in use (ErrorKind::InUse), and a lease that ends while the future runs, atmax_holdor as the store closes, fails itlost(ErrorKind::Lost). Holding and waiting and When a lease is lost say how. A hold in a task that you spawn waits like any other holder. - If you drop the hold's future while it waits, it leaves the line, so
tokio::time::timeout(d, summaries.hold(…))works besidewait. - If you keep the lease after the hold, by returning it or by moving it into
a task,
lease.lost()returns true once the hold ends. Another holder may have the key by then.
Reference
Bun and Node · Python · Go · Rust