nestrs-locks 1.7.1

Distributed lease locks for nestrs — NestJS @nestjs/locks parity.
Documentation

nestrs-locks

Lease locks for nestrs — NestJS @nestjs/locks analogue.

Use for single-runner cron, leader election–style critical sections, and any work that must not overlap across process replicas. Locks expire after a TTL so a crashed holder cannot block forever.

Install

[dependencies]
nestrs-locks = "1.7.1"

Or via the umbrella:

nestrs = { version = "1.7.1", features = ["locks"] }
# Redis backend (optional):
# nestrs-locks = { version = "1.7.1", features = ["cache-redis"] }

Register

use nestrs_locks::{LocksModule, LocksOptions};

#[module(imports = [LocksModule::register(LocksOptions::default())])]
struct AppModule;

Acquire

use nestrs_locks::LocksService;
use std::sync::Arc;

async fn nightly(locks: Arc<LocksService>) {
    if let Some(_guard) = locks.try_acquire("cron:nightly").await {
        // only one replica runs this
        do_work().await;
    } // drop guard → release

    // or:
    let _ = locks
        .with_lock("invoice:batch", || async { reconcile().await })
        .await;
}

async fn do_work() {}
async fn reconcile() {}

try_acquire returns None if another holder owns the name. with_lock runs the closure only when the lock is obtained.

Surface

  • LocksModule / LocksOptions / LocksService
  • LockGuard (RAII release)
  • InMemoryLockStore / SharedInMemoryLockStore
  • RedisLockStore (cache-redis)
  • LockStore trait for custom backends

Docs

License

MIT OR Apache-2.0.