passkey

3.0.0 • Public • Published

passkey

Shared lock built on top of redis

Reliable locking mechanism on Redis. Performs atomic locking and verifies ownership.

Build Status Dependency Status

Usage

var redis = require('redis');
var passkey = require('passkey');
 
var client = redis.createClient();
var key = passkey(client, {
  ttl: 10000
});
 
key.lock('my-lock')
  .then(function (lock) {
 
    return doWork()
      .then(function () {
        // extend the lock
        return lock.ttl(10000);
      })
      .then(function () {
        return doMoreWork();
      })
      .then(function () {
        // release the lock
        return lock.unlock();
      });
 
  })
  .catch(passkey.LockError, function (err) {
    // lock not obtained or expired before ttl/unlock was called
  })

API

var key = passkey(client, [options])

creates a new key that can be used to set locks, expects to be called with a redis client. Options can be an object with following properties

  • ttl: time before the lock automatically expires, default: 10000ms

key.lock(key, [ttl])

lock a key, any future call to .lock with the same value will fail with a promise.LockError. The lock will expire after ttl or the default time when unspecified. returns a Lock instance.

Lock.unlock()

Release this lock so it can be obtained by another client. Fails with a promise.LockError if the lock is expired.

Lock.ttl(ttl)

Set a new ttl on this lock. This is useful to extend the lifetime of the lock. Fails with a promise.LockError if the lock is expired.

Readme

Keywords

Package Sidebar

Install

npm i passkey

Weekly Downloads

8

Version

3.0.0

License

MIT

Last publish

Collaborators

  • janpotoms