OxL Reference

fn lock

Runs body under the distributed lock key (§12.6).

Example:

oxl✓ checked in CI
use std.cluster

fn main() io, net, time {
  let r = cluster.lock("report") { 42 }
  match r {
    i64 v => print("got {v}")
    LockTimeout _ => print("busy")
    NetError _ => print("unavailable")
  }
}
Output
got 42
pub(api) fn lock<T, effect F>(key: str, ttl: Duration = 30s, wait: Duration = 60s, body: fn() -> T F) -> T | LockTimeout | NetError  net, time, F

Thread safety. the lock is held by the node, not by a task; do not nest `lock` calls with the same key.

Since. 1.0.0-beta

Parameters

NameType
keystr
ttlDuration = 30s
waitDuration = 60s
bodyfn() -> T F

Returns

T

Errors

  • LockTimeout
  • NetError net, time, F
  • `LockTimeout` when the lock is not free within `wait`; `NetError` (`Unavailable`) when the lock service cannot be reached.

Effects

none declared (pure unless the body says otherwise)

Defined in: std/cluster/cluster.oxl:201

See also