etcd3 API - v2.0.0
    Preparing search index...

    Class Lease

    Lease is a high-level manager for etcd leases. Leases are great for things like service discovery:

    import * as os from 'node:os';
    import { Etcd3 } from '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));
    }

    Hierarchy

    • EventEmitter
      • Lease
    Index
    • Parameters

      • pool: ConnectionPool
      • namespace: NSApplicator
      • ttl: number
      • options: ILeaseOptions = {}

      Returns Lease

    • 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.

      Returns Promise<string>

    • 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:

      • We've not been able to contact etcd for a while and our TTL has definitely expired (emits a EtcdLeaseInvalidError)
      • We contacted etcd and it said that the lease was expired, or revoked (emits a EtcdLeaseInvalidError).
      • We weren't able to get an initial grant for the lease. This is NOT fired if revoke() is called manually.

      Parameters

      • event: "lost"
      • handler: (err: EtcdError) => void

      Returns this

    • keepaliveFired is emitted whenever we start trying to send a lease keepalive.

      Parameters

      • event: "keepaliveFired"
      • handler: () => void

      Returns this

    • keepaliveSucceeded is emitted when we successfully hit etcd with a keepalive for this lease.

      Parameters

      Returns this

    • 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.

      Parameters

      Returns this

    • keepaliveEstablished is emitted when a stream opens that we'll use for keepalives. This is mostly for testing.

      Parameters

      • event: "keepaliveEstablished"
      • handler: () => void

      Returns this

    • Put returns a put builder that operates within the current lease.

      Parameters

      • key: string | Buffer<ArrayBufferLike>

      Returns PutBuilder

    • 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.

      Returns void

    • Revoke frees the lease from etcd. Keys that the lease owns will be evicted.

      Parameters

      • options: CallOptions | undefined = ...

      Returns Promise<void>

    • Returns whether etcd has told us that this lease revoked.

      Returns boolean