passkey
v3.0.0
Published
Shared lock on top of redis
Downloads
31
Readme
passkey
Shared lock built on top of redis
Reliable locking mechanism on Redis. Performs atomic locking and verifies ownership.
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.