Acquire attempts to acquire the lock, rejecting if it's unable to.
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.
Returns the lease associated with this lock, if any. Returns null if the lock has not been acquired.
Sets the GRPC call options for this request.
Release frees the lock.
Sets the TTL of the lease underlying the lock. The lease TTL defaults to 30 seconds.
Generated using TypeDoc
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(); });