
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@nullplatform/k8s-lease-lock
Advanced tools
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
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.
To use the K8SLock module in your Node.js project, you can install it via npm:
npm install @nullplatform/k8s-lease-lock
const { K8SLock } = require("@nullplatform/k8s-lease-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.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.To stop the locking process, you can use the stopLocking
method, passing the lockId
obtained from startLocking
.
lock.stopLocking(lockInfo.lockId);
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);
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.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);
})();
This module is distributed under the MIT License. You can find more details in the LICENSE file.
Contributions and bug reports are welcome! Please feel free to open issues or pull requests on the GitHub repository.
FAQs
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
The npm package @nullplatform/k8s-lease-lock receives a total of 77 weekly downloads. As such, @nullplatform/k8s-lease-lock popularity was classified as not popular.
We found that @nullplatform/k8s-lease-lock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.