git clone https://github.com/bitfinexcom/bfx-facs-redis-lock
cd bfx-facs-redis-lock
git remote add upstream https://github.com/bitfinexcom/bfx-facs-base
In your service, run:
npm i --save https://github.com/bitfinexcom/bfx-facs-redis-lock
RedisLock is a class that extends from Facility, the main class in bfx-facs-base.
This Facility encapsulates the node-redlock libary.
Redlock can use node redis, ioredis redis library to keep its client connections.
// Instance an object
const RedLockFac = require('bfx-facs-redis-lock')
// Create a redis client (Redlock can use node redis, ioredis or any other compatible redis library to keep its client connections.)
const redisClient = require('redis').createClient(6379, 'redis1.example.com');
// Pass redis client to redlock
const redLockFac = new RedLockFac (caller, {
redis_client : redisClient
}, ctx)
// Within a worker constructor
this.setInitFacs([
// Start a Redis client First.
['fac', 'bfx-facs-redis', '0', '0',{}, -4],
['fac', 'bfx-facs-redis-lock', '0', '0', () => {
// Pass a Redis client to Redis Lock Fac
return {
redis_client: this.redis_0.cli_rw
}
}, -3]
])
resource (string)
resource to be lockedttl (number)
time in ms until the lock expirescallback (function)
callback returning:err (Error)
lock (Lock)
lock (Lock)
lock to be releasedcallback (function)
callback returning:err (Error)
lock (Lock)
lock to be extendedttl (number)
time in ms to extend the lock's expirationcallback (function)
callback returning:err (Error)
lock (Lock)
resource (string)
resource to be lockedttl (number)
time in ms to extend the lock's expirationcallback (function)
error handler called with:err (Error)
callback (function)
error handler called with:err (Error)
*[]
results of calling.quit()
on each client
callback (function)
callback returning:err (Error)
ttl (number)
time from now in ms to set as the lock's new expirationcallback (function)
callback returning:err (Error)
lock (Lock)
// the string identifier for the resource you want to lock
var resource = 'locks:account:322456';
// the maximum amount of time you want the resource locked,
// keeping in mind that you can extend the lock up until
// the point when it expires
var ttl = 1000;
redlock.lock(resource, ttl, function(err, lock) {
// we failed to lock the resource
if(err) {
// ...
}
// we have the lock
else {
// ...do something here...
// unlock your resource when you are done
lock.unlock(function(err) {
// we weren't able to reach redis; your lock will eventually
// expire, but you probably want to log this error
console.error(err);
});
}
});
redlock.lock('locks:account:322456', 1000).then(function(lock) {
// ...do something here...
// if you need more time, you can continue to extend
// the lock as long as you never let it expire
// this will extend the lock so that it expires
// approximitely 1s from when `extend` is called
return lock.extend(1000).then(function(lock){
// ...do something here...
// unlock your resource when you are done
return lock.unlock()
.catch(function(err) {
// we weren't able to reach redis; your lock will eventually
// expire, but you probably want to log this error
console.error(err);
});
});
});