Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
node-redlock
Advanced tools
A distributed locking algorithm used to manage distributed resources in a system.
This library provides a robust and distributed locking mechanism using the Redlock algorithm implemented in TypeScript. It allows you to acquire, release, and renew locks on shared resources across multiple Redis instances.
Redlock.ts
file into your project.npm install ioredis
Create Redis clients that implement the RedisClient
interface and pass them to the Redlock
class. For example, using ioredis
:
import Redis from 'ioredis';
import { Redlock } from './Redlock';
const redisClients = [
new Redis({ host: '127.0.0.1', port: 6379 }),
new Redis({ host: '127.0.0.1', port: 6380 }),
new Redis({ host: '127.0.0.1', port: 6381 }),
];
const redlock = new Redlock(redisClients);
You can acquire a lock on a resource with a specific TTL:
(async () => {
const lock = await redlock.acquireLock('my-resource', 5000); // Lock for 5000ms
if (lock) {
console.log('Lock acquired:', lock);
} else {
console.log('Failed to acquire lock');
}
})();
To customize the retry strategy, pass a function to acquireLockWithCustomRetry
:
(async () => {
const retryStrategy = (attempt: number) => 100 * (attempt + 1); // Exponential backoff
const lock = await redlock.acquireLockWithCustomRetry('my-resource', 5000, retryStrategy);
if (lock) {
console.log('Lock acquired with custom retry:', lock);
} else {
console.log('Failed to acquire lock with custom retry');
}
})();
To release a lock after use:
(async () => {
const lock = await redlock.acquireLock('my-resource', 5000);
if (lock) {
await redlock.releaseLock(lock.resource, lock.value);
console.log('Lock released');
}
})();
Extend the TTL of an existing lock:
(async () => {
const lock = await redlock.acquireLock('my-resource', 5000);
if (lock) {
const renewed = await redlock.renewLock(lock.resource, lock.value, 5000);
if (renewed) {
console.log('Lock renewed');
} else {
console.log('Failed to renew lock');
}
}
})();
acquireLock(resource: string, ttl: number): Promise<Lock | null>
Attempts to acquire a lock on the specified resource.
Parameters:
resource
: The resource to lock.ttl
: The time-to-live for the lock in milliseconds.Returns: A Lock
object if successful, otherwise null
.
acquireLockWithCustomRetry(resource: string, ttl: number, retryStrategy: (attempt: number) => number): Promise<Lock | null>
Attempts to acquire a lock using a custom retry strategy.
Parameters:
resource
: The resource to lock.ttl
: The time-to-live for the lock in milliseconds.retryStrategy
: A function that determines the delay before the next retry attempt.Returns: A Lock
object if successful, otherwise null
.
releaseLock(resource: string, value: string): Promise<void>
Releases a lock on the specified resource.
Parameters:
resource
: The resource to unlock.value
: The unique lock value.Returns: A Promise
that resolves when the lock is released.
renewLock(resource: string, value: string, ttl: number): Promise<boolean>
Renews the TTL for an existing lock.
Parameters:
resource
: The resource to renew the lock on.value
: The unique lock value.ttl
: The new TTL in milliseconds.Returns: true
if the lock was successfully renewed, otherwise false
.
value
for each lock to prevent accidental unlocking by other processes.This project is licensed under the MIT License. See the LICENSE file for details.
FAQs
A distributed locking algorithm used to manage distributed resources in a system.
The npm package node-redlock receives a total of 58 weekly downloads. As such, node-redlock popularity was classified as not popular.
We found that node-redlock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.