npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nullplatform/k8s-lease-lock

v0.1.4

Published

The **K8SLock** module is a Node.js library designed to provide distributed locking functionality using Kubernetes leases. It allows you to manage locks in a Kubernetes cluster, ensuring that only one client or process can hold a lock at any given time. T

Downloads

859

Readme

K8SLock Module

The K8SLock module is a Node.js library designed to provide distributed locking functionality using Kubernetes leases. It allows you to manage locks in a Kubernetes cluster, ensuring that only one client or process can hold a lock at any given time. This is useful for scenarios where you need to coordinate tasks across multiple instances of your application or ensure exclusive access to shared resources.

Installation

To use the K8SLock module in your Node.js project, you can install it via npm:

npm install @nullplatform/k8s-lease-lock

Usage

Importing the Module

const { K8SLock } = require("@nullplatform/k8s-lease-lock");

Creating a Lock

To create a lock, you need to instantiate the K8SLock class with the required configuration options. Here's an example:

const lock = new K8SLock({
  leaseName: "test-lease-pepe",
  namespace: "n1",
  lockLeaserId: "colo2",
  leaseDurationInSeconds: 50,
});
  • leaseName: The name of the lease resource used for locking.
  • namespace: The Kubernetes namespace where the lease should be created.
  • lockLeaserId: An identifier for the entity acquiring the lock.
  • leaseDurationInSeconds: The duration (in seconds) for which the lock should be held.

Starting Locking

To initiate the locking process, you can call the startLocking method. This method will continuously attempt to acquire and maintain the lock.

const lockInfo = await lock.startLocking();
console.log("Locking started:", lockInfo.isLocking);
  • lockInfo.isLocking: Indicates whether the lock has been acquired or not.
  • lockInfo.lockId: An interval ID that can be used to stop the locking process.

Stopping Locking

To stop the locking process, you can use the stopLocking method, passing the lockId obtained from startLocking.

lock.stopLocking(lockInfo.lockId);

Getting a Lock

You can also explicitly attempt to acquire a lock using the getLock method. If waitUntilLock is set to true, it will keep trying until the lock is acquired.

const locked = await lock.getLock(true);
console.log("Lock acquired:", locked);

Configuration Options

The K8SLock constructor accepts several configuration options:

  • kubeConfig: A Kubernetes configuration object. If not provided, it will load the default configuration.
  • createLeaseIfNotExist: If true, the module will create a lease if it doesn't already exist.
  • labels: Labels to apply to the lease when creating it.
  • refreshLockInterval: Interval (in milliseconds) for refreshing the lease.
  • lockTryInterval: Interval (in milliseconds) for retrying to acquire the lock.

Example

Here's an example of how to use the K8SLock module to acquire a lock:

const { K8SLock } = require("@k8s-lock");

(async () => {
  const lock = new K8SLock({
    leaseName: "test-lease-pepe",
    namespace: "n1",
    lockLeaserId: "colo2",
    leaseDurationInSeconds: 50,
  });

  const lockInfo = await lock.startLocking();
  console.log("Locking started:", lockInfo.isLocking);
})();

License

This module is distributed under the MIT License. You can find more details in the LICENSE file.

Contributions

Contributions and bug reports are welcome! Please feel free to open issues or pull requests on the GitHub repository.