Expand description
Distributed locks.
A LockManager grants mutual exclusion on a string key with a time-to-live
(TTL), so a crashed holder cannot wedge the lock forever — it expires.
acquire returns Some(guard) to the winner and
None when the key is already held by a live (unexpired) lock. The
LockGuard releases the lock when dropped, or eagerly via
release.
Expiry is driven by an injected Clock, so tests can pin and advance time
with a FixedClock instead of sleeping.
This module provides the trait, the LockGuard model, and an in-memory
implementation. A Redis-backed manager (SET key token NX PX ttl, released
with a compare-and-delete Lua script) is a future pass.
use std::sync::Arc;
use klauthed_core::time::Duration;
use klauthed_core::time::SystemClock;
use klauthed_data::locks::{InMemoryLockManager, LockManager};
let locks = InMemoryLockManager::new(Arc::new(SystemClock));
if let Some(guard) = locks.acquire("job:nightly", Duration::seconds(30)).await? {
// critical section …
guard.release().await?;
}Modules§
- mongo
- MongoDB-backed
LockManagerusing compare-and-upsert with TTL. - redis
- Redis-backed
LockManager.
Structs§
- InMemory
Lock Manager - A thread-safe, in-memory
LockManagerfor tests and single-process use. - Lock
Guard - A held lock. Dropping it releases the lock;
releasedoes so eagerly and lets the caller observe errors. - Lock
Token Tag - Marker tag for a lock’s fencing token.
Traits§
- Lock
Manager - A manager that grants mutually-exclusive, TTL-bounded locks by key.
Type Aliases§
- Lock
Token - A unique token identifying a single acquisition of a lock. Used as a fencing token so a release only frees the acquisition that created it.