Concrete implementation of a Kubernetes based time-based mutually exclusive lock via the Coordination API. Applies a namespace/deployment wide lock to ensure that only one process, machine, and user can hold the lease at a time. The lease is automatically renewed in the background to prevent expiration and ensure the holder maintains the lease. If the process die, the lease is automatically released after the lease duration.

Implements

Constructors

  • Parameters

    • client: K8

      Injected kubernetes client need by the methods to create, renew, and delete leases.

    • renewalService: LeaseRenewalService

      Injected lease renewal service need to support automatic (background) lease renewals.

    • leaseHolder: LeaseHolder

      The holder of the lease.

    • namespace: string

      The namespace in which the lease is to be acquired.

    • leaseName: string = null

      The name of the lease to be acquired; if not provided, the namespace is used.

    • durationSeconds: number = null

      The duration in seconds for which the lease is to be held; if not provided, the default value is used.

    Returns IntervalLease

Properties

client: K8

Injected kubernetes client need by the methods to create, renew, and delete leases.

renewalService: LeaseRenewalService

Injected lease renewal service need to support automatic (background) lease renewals.

DEFAULT_LEASE_DURATION: 20

The default duration in seconds for which the lease is to be held before being considered expired.

Accessors

  • get durationSeconds(): number
  • The duration in seconds for which the lease is held before being considered expired. By default, the duration is set to 20 seconds. It is recommended to renew the lease at 50% of the duration to prevent unexpected expiration.

    Returns number

  • get namespace(): string
  • The namespace in which the lease is to be acquired. By default, the namespace is used as the lease name. The defaults assume there is only a single deployment in a given namespace.

    Returns string

Methods

  • Acquires the lease. If the lease is already acquired, it checks if the lease is expired or held by the same process. If the lease is expired, it creates a new lease. If the lease is held by the same process, it renews the lease. If the lease is held by another process, then an exception is thrown.

    Returns Promise<void>

    LeaseAcquisitionError - If the lease is already acquired by another process or an error occurs during acquisition.

  • Checks if the lease is acquired. If the lease is acquired and not expired, it returns true; otherwise, false.

    Returns Promise<boolean>

    true if the lease is acquired and not expired; otherwise, false.

  • Checks if the lease is expired. If the lease is expired, it returns true; otherwise, false. This method does not verify if the lease is acquired by the current process.

    Returns Promise<boolean>

    true if the lease is expired; otherwise, false.

  • Releases the lease. If the lease is expired or held by the same process, it deletes the lease. If the lease is held by another process, then an exception is thrown.

    Returns Promise<void>

    LeaseRelinquishmentError - If the lease is already acquired by another process or an error occurs during relinquishment.

  • Renews the lease. If the lease is expired or held by the same process, it creates or renews the lease. If the lease is held by another process, then an exception is thrown.

    Returns Promise<void>

    LeaseAcquisitionError - If the lease is already acquired by another process or an error occurs during renewal.

  • Attempts to acquire the lease, by calling the acquire method. If an exception is thrown, it is caught and false is returned. If the lease is successfully acquired, true is returned; otherwise, false is returned.

    Returns Promise<boolean>

    true if the lease is successfully acquired; otherwise, false.

  • Attempts to release the lease, by calling the release method. If an exception is thrown, it is caught and false is returned. If the lease is successfully released, true is returned; otherwise, false is returned.

    Returns Promise<boolean>

    true if the lease is successfully released; otherwise, false.

  • Attempts to renew the lease, by calling the renew method. If an exception is thrown, it is caught and false is returned. If the lease is successfully renewed, true is returned; otherwise, false is returned.

    Returns Promise<boolean>

    true if the lease is successfully renewed; otherwise, false.