Coder Social home page Coder Social logo

zk-lock's Introduction

zk-lock

Distributed locking using node-zookeeper-client, following the recipe in the zookeeper documentation.

The library exposes a single class, ZookeeperLock which is both the lock object, and provides static methods for initializing the configuration and acquiring locks which are already locked.

All locks are stored in a top level zookeeper folder locks/, can have an optional path prefix, and take a key argument which indicates the resource that should be locked.

Locks must be explicitly released with the unlock method in order to allow other processes to obtain the lock. unlock will also close the zookeeper connection.

Configuration

serverLocator: () => Promise<any> serverLocator is a service discovery/location resolving library such as locators, or anything which implements a function which takes no arguments and can return a promise of a Location object that has properties host and port which specify where the zookeeper server can be discovered.

pathPrefix: string This is a prefix to add to any key which is to be locked. Note: all locks will start with the path locks/, so with a prefix of foo/bar, when locking the key baz the zookeeper path will be of the form locks/foo/bar/baz.

sessionTimeout: number node-zookeeper-client sessionTimeout parameter that is passed down to the zookeeper client.

spinDelay: number node-zookeeper-client spinDelay parameter that is passed down to the zookeeper client.

retries: number node-zookeeper-client retries parameter that is passed down to the zookeeper client.

failImmediate: boolean when set to true, instead of a blocking call to .lock, it will fail immediately throwing a ZookeeperLockAlreadyLockedError

maxConcurrentHolders: number allow multiple calls to .lock to grab a lock, allowing the lock to control the number of participants in an action

ZookeeperLock

constructor

usage:

	var lock = new ZookeeperLock(config);
  

Static methods

initialize

usage: ZookeeperLock.initialize(config);

Initialize a global configuration for zookeeper locks.

lockFactory

usage:

	var lock = ZookeeperLock.lockFactory();

lock

usage:

	ZookeeperLock.lock('key').then(function (lock) {
    	... do stuff
  	});

Instance methods

lock

usage:

    var lock = ZookeeperLock.lockFactory();
    lock.lock('key').then(function() {
    	... do stuff
  	});

unlock

usage;

    var lock = ZookeeperLock.lockFactory();
    lock.lock('key').then(function() {
    	... do stuff
    	lock.unlock().then(function () {
    	    ... 
    	});
    });

Session timeouts

Due to the nature of zookeeper, this locking strategy is susceptible to a situation when sessionTimeout occurs that a lock holder can still think that they own the lock, but the key has been lost on the zookeeper server. To handle this, ZookeeperLock exposes a signal property that emits a lost event when a sessionTimeout occurs, which MUST be handled by the lock holder to terminate execution.

usage:

    var lock = ZookeeperLock.lockFactory();
    lock.lock('key').then(function() {
    	lock.on('lost', function () {
    		... handle losing the lock, i.e. restart locking cycle.
    	});
    });

Development

npm run build to compile.

The tests currently require a local installation of zookeeper to run successfully, specified by the zkServerCommandPath variable which is defined near the beginning of the tests.

zk-lock's People

Contributors

clintropolis avatar desaianuj avatar lambertkuang avatar vilicvane avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

vilicvane

zk-lock's Issues

一个连接只能由一个锁吗

我看代码,发现,一个ZookeeperLock即一个zookeeper连接只能锁一个key?
一个client可以复用不同的key吗?

How to handle multiple ZookeeperLock instance lock same key?

Hi:
When I use multiple ZookeeperLock instance lock same key, they can lock the same key successfully, It is not what we expect, right? If so , how to handle this? Because in distributed system, it is normal for this case, thanks for you reply in advance

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.