Grant waits for the lease to be granted. You generally don't need to
call this, as any operations with .put
will queue automatically.
Calling this multiple times is safe; it won't try to request multipl leases.
It rejects if the lease cannot be granted, in additon to the lost
event firing.
keepaliveOnce fires an immediate keepalive for the lease.
A lost
event is fired when etcd indicates that we've lost the lease
on this client. This can be a result of a number of events:
revoke()
is called manually.keepaliveFired is emitted whenever we start trying to send a lease keepalive.
keepaliveSucceeded is emitted when we successfully hit etcd with a keepalive for this lease.
keepaliveFailed is emitted when an error happens in the keepalive loop.
We may be able to recover (e.g. by connecting to a different server),
the lease should not be considered revoked until lost
is emitted.
keepaliveEstablished is emitted when a stream opens that we'll use for keepalives. This is mostly for testing.
Put returns a put builder that operates within the current lease.
releasePassively stops making heartbeats for the lease, and allows it
to expire automatically when its TTL rolls around. Use revoke()
to
actively tell etcd to terminate the lease.
Revoke frees the lease from etcd. Keys that the lease owns will be evicted.
Returns whether etcd has told us that this lease revoked.
Generated using TypeDoc
Lease is a high-level manager for etcd leases. Leases are great for things like service discovery:
const os = require('os'); const { Etcd3 } = require('etcd3'); const client = new Etcd3(); const hostPrefix = 'available-hosts/'; function grantLease() { const lease = client.lease(10); // set a TTL of 10 seconds lease.on('lost', err => { console.log('We lost our lease as a result of this error:', err); console.log('Trying to re-grant it...'); grantLease(); }) await lease.put(hostPrefix + os.hostname()).value(''); } function getAvailableHosts() { const keys = await client.getAll().prefix(hostPrefix).keys(); return keys.map(key => key.slice(hostPrefix.length)); }