Name: redfour
Owner: nodemailer
Description: A redis binary semaphore with async wait
Forked from: mixmaxhq/redfour
Created: 2017-10-03 07:11:44.0
Updated: 2018-04-16 18:27:58.0
Pushed: 2017-10-03 08:11:21.0
Homepage: null
Size: 38
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Originally forked from redfour. Main difference being that redfour uses node_redis + node-redis-scripty while ioredfour uses ioredis.
or
install ioredfour --save
Lock = require('ioredfour');
testLock = new Lock({
Can also be an `Object` of options to pass to `new Redis()`
https://www.npmjs.com/package/ioredis#connect-to-redis, or an existing
instance of `ioredis` (if you want to reuse one connection, though this
module must create a second).
dis: 'redis://localhost:6379',
mespace: 'mylock'
id = Math.random();
firstlock;
irst, acquire the lock.
Lock.acquireLock(id, 60 * 1000 /* Lock expires after 60sec if not released */ , function(err, lock) {
(err) {
console.log('error acquiring', err);
else if (!lock.success) {
console.log('lock exists', lock);
else {
console.log('lock acquired initially');
firstlock = lock;
nother server might be waiting for the lock like this.
Lock.waitAcquireLock(id, 60 * 1000 /* Lock expires after 60sec */ , 10 * 1000 /* Wait for lock for up to 10sec */ , function(err, lock) {
(err) {
console.log('error wait acquiring', err);
else {
console.log('lock acquired after wait!', lock);
hen the original lock is released, `waitAcquireLock` is fired on the other server.
imeout(() => {
stLock.releaseLock(firstlock, (err) => {
if (err) {
console.log('error releasing', err);
} else {
console.log('released lock');
}
;
* 1000);
We welcome pull requests! Please lint your code.
instanceof
to determine if the redis
constructor option is oftype `redis.RedisClient`.
Shortened (and easier to pronouce) version of “Redis Semaphore”