Options
All
  • Public
  • Public/Protected
  • All
Menu

A Lock can be used for distributed locking to create atomic operations across multiple systems. An EtcdLockFailedError is thrown if the lock can't be acquired.

Under the hood, the Lock uses a lease on a key which is revoked when the the lock is released. If the server the lock is running on dies, or the network is disconnected, etcd will time out the lock.

Bear in mind that this means that in certain rare situations (a network disconnect or wholesale etcd failure), the caller may lose the lock while operations may still be running.

A quick example:

const { Etcd3 } = require('etcd3');
const client = new Etcd3();

client.lock('my_resource').do(() => {
  // The lock will automatically be released when this promise returns
  return doMyAtomicAction();
});

Hierarchy

  • Lock

Index

Constructors

constructor

Methods

acquire

  • acquire(): Promise<this>
  • Acquire attempts to acquire the lock, rejecting if it's unable to.

    Returns Promise<this>

do

  • do<T>(fn: () => T | Promise<T>): Promise<T>
  • do() wraps the inner function. It acquires the lock before running the function, and releases the lock after any promise the function returns resolves or throws.

    Type parameters

    • T

    Parameters

    • fn: () => T | Promise<T>
        • (): T | Promise<T>
        • Returns T | Promise<T>

    Returns Promise<T>

leaseId

  • leaseId(): Promise<string | null>
  • Returns the lease associated with this lock, if any. Returns null if the lock has not been acquired.

    Returns Promise<string | null>

options

  • options(options: CallOptions): this
  • Sets the GRPC call options for this request.

    Parameters

    • options: CallOptions

    Returns this

release

  • release(): Promise<void>

ttl

  • ttl(seconds: number): this
  • Sets the TTL of the lease underlying the lock. The lease TTL defaults to 30 seconds.

    Parameters

    • seconds: number

    Returns this

Legend

  • Constructor
  • Method
  • Inherited method

Generated using TypeDoc