Giter Club home page Giter Club logo

zk-distributed-lock's Introduction

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

zk-distributed-lock's People

Contributors

kunalburangi avatar

Watchers

 avatar

zk-distributed-lock's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.