Skip to main content

Module locks

Module locks 

Source
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 LockManager using compare-and-upsert with TTL.
redis
Redis-backed LockManager.

Structs§

InMemoryLockManager
A thread-safe, in-memory LockManager for tests and single-process use.
LockGuard
A held lock. Dropping it releases the lock; release does so eagerly and lets the caller observe errors.
LockTokenTag
Marker tag for a lock’s fencing token.

Traits§

LockManager
A manager that grants mutually-exclusive, TTL-bounded locks by key.

Type Aliases§

LockToken
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.