https://github.com/vanng822/rlock
Distributed lock for nodejs using redis
https://github.com/vanng822/rlock
Last synced: 2 months ago
JSON representation
Distributed lock for nodejs using redis
- Host: GitHub
- URL: https://github.com/vanng822/rlock
- Owner: vanng822
- License: mit
- Created: 2014-01-04T15:36:31.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2014-03-01T00:48:38.000Z (over 12 years ago)
- Last Synced: 2025-12-26T10:47:17.230Z (6 months ago)
- Language: JavaScript
- Homepage:
- Size: 301 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
rlock
=====
Distributed lock for nodejs using redis
## Usage example
var rlock = require('rlock');
var lock = new rlock.Lock('rlock.key');
lock.acquire(function(err, done) {
if (!done) {
return console.log('Could not acquire lock');
}
// Do stuffs
done();
// OR
done(function(err, ok){
console.log(ok);
});
// OR
lock.release(function(err, ok){
console.log(ok);
});
});
// Set global redis client, with your own setting
var rlock = require('rlock');
var redis = require('redis');
rlock.setRedisClient(redis.createClient(6378));
## API
### Lock(key, options)
* `key` String
* `options` Object, optional.
Available option:
* `maxRetries` Maximum number of retries if can not acquire the lock, specify 0 if no retry, default 10.
* `retryDelay` Number of milliseconds to wait until next try, default 50 milliseconds.
* `timeout` Number of milliseconds before the lock expires, default 5000 milliseconds.
* `rclient` Instance of redis client, see https://github.com/mranney/node_redis#rediscreateclientport-host-options. If not specified it will create a client with default config (or the global one if set). Be aware that redis.createClient crashes on error if not handle.
#### Lock.acquire(callback)
* `callback` Function(err, done) success if and only if done is a function. Can use it to release the lock.
#### Lock.release(callback)
* `callback` Function(err, ok) ok `true` if success. Callback is optional. This can only call if Lock.acquire was successful.
### setRedisClient(client)
* `client` Instance of redis client, see https://github.com/mranney/node_redis#rediscreateclientport-host-options When no rclient specified in options this will be used.