function Redlock(clients, options) { // set default options options = options || {}; this.driftFactor = typeof options.driftFactor === 'number' ? options.driftFactor : defaults.driftFactor; this.retryCount = typeof options.retryCount === 'number' ? options.retryCount : defaults.retryCount; this.retryDelay = typeof options.retryDelay === 'number' ? options.retryDelay : defaults.retryDelay; this.lockScript = typeof options.lockScript === 'function' ? options.lockScript(lockScript) : lockScript; this.unlockScript = typeof options.unlockScript === 'function' ? options.unlockScript(unlockScript) : unlockScript; this.extendScript = typeof options.extendScript === 'function' ? options.extendScript(extendScript) : extendScript; // set the redis servers from additional arguments this.servers = clients; if(this.servers.length === 0) throw new Error('Redlock must be instantiated with at least one redis server.'); }
n/a
function Lock(redlock, resource, value, expiration) { this.redlock = redlock; this.resource = resource; this.value = value; this.expiration = expiration; }
n/a
function LockError(message) { Error.call(this); Error.captureStackTrace(this, LockError); this.name = 'LockError'; this.message = message || 'Failed to lock the resource.'; }
n/a
function Error() { [native code] }
n/a
function EventEmitter() { EventEmitter.init.call(this); }
n/a
function Lock(redlock, resource, value, expiration) { this.redlock = redlock; this.resource = resource; this.value = value; this.expiration = expiration; }
n/a
function extend(ttl, callback) { return this.redlock.extend(this, ttl, callback); }
...
```js
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
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
...
function unlock(callback) { return this.redlock.unlock(this, callback); }
...
var ttl = 1000;
redlock.lock(resource, ttl).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);
});
});
...
function LockError(message) { Error.call(this); Error.captureStackTrace(this, LockError); this.name = 'LockError'; this.message = message || 'Failed to lock the resource.'; }
n/a
function Error() { [native code] }
n/a
function Error() { [native code] }
n/a
function captureStackTrace() { [native code] }
...
// LockError
// ---------
// This error is returned when there is an error locking a resource.
function LockError(message) {
Error.call(this);
Error.captureStackTrace(this, LockError);
this.name = 'LockError';
this.message = message || 'Failed to lock the resource.';
}
util.inherits(LockError, Error);
...