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

zk-distributed-lock

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

zk-distributed-lock

ZK-Distributed-Lock

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

ZK-Distributed-Lock

Description

[pipeline status [coverage report

This project implements a Distributed Lock system using Zookeeper. The Project Allows you to acquire lock and to the task while other resources won't be able to acquire it at the same time.

Read more about zookeeper at. https://zookeeper.apache.org/

Contents

Getting_Started

Prerequisites

This project requires Node.js 10 or later.

Install

Install all required packages with

npm i zk-distributed-lock

Now compile the typescript code:

npm run build

Usage

Create an instance of the DistributedLock class

  this.lock = new DistributedLock(zkConnectString, this.lockPath);

Register the events handler if you want.

Acquire and release the lock the Lock,

    await this.lock.acquireLock( (error) => {
      if (error) {
        console.error('Failed to acquire lock:', error);
        return;
      }

      console.log('Lock acquired, doing some work...');
      this.response = "Lock Acquired";
      // Do some work here...

      this.lock.releaseLock(this.lockPath, (error) => {
        if (error) {
          console.error('Failed to release lock:', error);
          return 'Failed to release lock:';
        } else {
          console.log('Lock released');
          this.response = "Lock released";

          return 'task done';
        }
      });
      return 'task done';
    });

Optional parameter

 const optionsRetry:DistributedLockOptions = {
    logger: logger, // your custom logger
    retryCount: 2, // number of times it should retry. put  0 is default
    retryAfter: 2000 , // time in miliseconds between retries.  0 is default
  };

After this even if multiple Instances of your applications are running still only one resource will be able to acquire the lock.

Testing

npm run test:exit

Unit Tests

Unit tests are using

  • Mocha as framework
  • chai as assertion library
  • nyc for test coverage detection.

In order to run just the mocha tests, use

npm run test

In order both run unit tests and test coverage detection, use:

npm run test:ci

Both command will automatically build the project before testing. Unit test results are reported by mochawesome in html format and stored in the root folder under /mochawesome-report. The coverage report can be found in html format in /coverage

Note : You need to have zookeeper running. Here's a docker image this i use

docker run --name my-zookeeper -p 2182:2181 -d zookeeper

Team

License

MIT

This repository was generated by tmpo

FAQs

Package last updated on 06 May 2023

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