ZK-Distributed-Lock
Description
[
[
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