Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-redlock

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-redlock

A distributed locking algorithm used to manage distributed resources in a system.

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
increased by120%
Maintainers
1
Weekly downloads
 
Created
Source

Redlock Implementation in TypeScript

This project provides a Redlock implementation in TypeScript, compatible with both ioredis and redisjs.

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd <repository-directory>
    
  2. Install dependencies:

    npm install
    

Usage

Initialize Redis Clients

Initialize both ioredis and redis clients:

import Redis from 'ioredis';
import { createClient } from 'redis';
import { IoredisClient } from './src/IoredisClient';
import { RedisjsClient } from './src/RedisjsClient';
import Redlock from './src/Redlock';

(async () => {
  const ioredisClients = [
    new Redis({ host: 'localhost', port: 6379 }),
    new Redis({ host: 'localhost', port: 6380 }),
    new Redis({ host: 'localhost', port: 6381 }),
  ].map(client => new IoredisClient(client));

  const redisjsClients = [
    createClient({ url: 'redis://localhost:6379' }),
    createClient({ url: 'redis://localhost:6380' }),
    createClient({ url: 'redis://localhost:6381' }),
  ];

  await Promise.all(redisjsClients.map(client => client.connect()));
  const redisjsClientInstances = redisjsClients.map(client => new RedisjsClient(client));

  const clients = [...ioredisClients, ...redisjsClientInstances];
  const redlock = new Redlock(clients);

  const resource = 'locks:example';
  const ttl = 10000; // 10 seconds

  const lock = await redlock.acquireLock(resource, ttl);

  if (lock) {
    console.log(`Acquired lock on ${resource}`);
    // Do your critical section work here
    await redlock.releaseLock(resource, lock.value);
    console.log(`Released lock on ${resource}`);
  } else {
    console.log(`Failed to acquire lock on ${resource}`);
  }

  ioredisClients.forEach(client => (client as IoredisClient).client.quit());
  await Promise.all(redisjsClients.map(client => client.quit()));
})();

Keywords

FAQs

Package last updated on 31 May 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc