Skip to main content

Crate ash_lock

Crate ash_lock 

Source
Expand description

§ash-lock — distributed coordination for the ash-* ecosystem

Status: the default build is single-process (InMemoryBackend). Real distributed locking is provided by the etcd feature (EtcdBackend: etcd leases + transactions, with cluster-wide monotonic revision-based fencing tokens). Some collaborators named in the original spec are still stubbed:

Spec dependencyStatus in this crate
lock backendInMemoryBackend (default) or EtcdBackend (etcd feature)
ash-telemetryreplaced by the in-crate telemetry counter module
ash-discoveryseeds are recorded but unused; LockClient::connect runs in-process
ash-time (HLC)adopted behind the ash-time feature: stamps every acquire — see clock
ash-flaresupervision contract honoured by the internal watchdog; no ash-flare dependency yet
ash-rpcremote backend transport not implemented

§Fencing tokens — READ THIS

A distributed lock cannot prevent a stalled lock holder (GC pause, paused VM) from waking up and writing after its lease has already expired and been re-granted. The only robust defence is a fencing token: a strictly monotonic number handed out on every acquire, which you attach to every write to the protected resource. The resource rejects any write whose token is lower than the highest it has seen. Use validate_write at the resource.

use ash_lock::{LockClient, LockConfig, validate_write};
use std::time::Duration;

let client = LockClient::connect(LockConfig {
    default_ttl: Duration::from_secs(30),
    watchdog_interval: Duration::from_secs(10),
    ..Default::default()
}).await?;

let guard = client.lock("orders/42").await.with_ttl(Duration::from_secs(20)).acquire().await?;
let token = guard.fencing_token()?;      // attach `token` to every write
assert!(validate_write(token, token));   // resource-side check
// ... do work ...
guard.unlock().await?;                    // or just drop `guard`

Re-exports§

pub use backend::InMemoryBackend;
pub use backend::LockBackend;
pub use clock::Stamp;

Modules§

backend
The LockBackend abstraction and its implementations.
clock
Causality clock used to stamp lock operations.
telemetry
Metrics for ash-lock.

Structs§

LockBuilder
Fluent configuration for a single acquire attempt.
LockClient
A handle to the distributed lock service.
LockConfig
Configuration for connecting a LockClient to a lock cluster.
LockGuard
A held distributed lock.

Enums§

Error
Errors that can occur while acquiring, renewing, or releasing a lock.

Functions§

validate_write
Validate that a write carrying token is still allowed against a resource whose last-seen fencing token is current_token.

Type Aliases§

Result
The result type used throughout the public API.