Options
All
  • Public
  • Public/Protected
  • All
Menu

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));
}

Hierarchy

  • EventEmitter
    • Lease

Index

Constructors

Accessors

Methods

Constructors

constructor

Accessors

state

Methods

grant

  • grant(): Promise<string>
  • 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>

keepaliveOnce

  • keepaliveOnce fires an immediate keepalive for the lease.

    Parameters

    • Default value options: CallOptions | undefined = this.defaultOptions

    Returns Promise<ILeaseKeepAliveResponse>

on

  • on(event: "lost", handler: (err: EtcdError) => void): this
  • on(event: "keepaliveFired", handler: () => void): this
  • on(event: "keepaliveSucceeded", handler: (res: ILeaseKeepAliveResponse) => void): this
  • on(event: "keepaliveFailed", handler: (res: ILeaseKeepAliveResponse) => void): this
  • on(event: "keepaliveEstablished", handler: () => void): this
  • 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

    Returns this

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

    Parameters

    • event: "keepaliveFired"
    • handler: () => void
        • (): void
        • Returns 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
        • (): void
        • Returns void

    Returns this

put

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

    Parameters

    • key: string | Buffer

    Returns PutBuilder

release

  • release(): void
  • 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

  • revoke(options?: CallOptions | undefined): Promise<void>
  • Revoke frees the lease from etcd. Keys that the lease owns will be evicted.

    Parameters

    • Default value options: CallOptions | undefined = this.defaultOptions

    Returns Promise<void>

revoked

  • revoked(): boolean
  • Returns whether etcd has told us that this lease revoked.

    Returns boolean

Legend

  • Constructor
  • Method
  • Inherited method

Generated using TypeDoc