node-redis-warlock
Advanced tools
Comparing version 1.0.0 to 1.0.1
const UUID = require('uuid'); | ||
const { createScript } = require('./del'); | ||
const { ParityDel, ParityRelock } = require('./scripts'); | ||
@@ -7,3 +7,4 @@ module.exports = function (redis) { | ||
const parityDel = createScript(redis); | ||
const parityDel = ParityDel(redis); | ||
const parityRelock = ParityRelock(redis); | ||
@@ -36,5 +37,3 @@ warlock.makeKey = function (key) { | ||
let unlock = warlock.unlock.bind(warlock, key, id); | ||
if (!lockSet) unlock = false; | ||
const unlock = lockSet ? warlock.unlock.bind(warlock, key, id) : false; | ||
return cb(err, unlock, id); | ||
@@ -94,3 +93,22 @@ }, | ||
warlock.touch = async (key, id, ttl, cb) => { | ||
if (typeof key !== 'string') { | ||
const e = new Error('lock key must be string'); | ||
e.id = id; | ||
e.key = key; | ||
e.ttl = ttl; | ||
if (!cb) throw e; | ||
return cb(e); | ||
} | ||
try { | ||
const result = await parityRelock(3, warlock.makeKey(key), ttl, id); | ||
return cb ? cb(null, result) : result; | ||
} catch (e) { | ||
if (!cb) throw e; | ||
return cb(e); | ||
} | ||
}; | ||
return warlock; | ||
}; |
{ | ||
"name": "node-redis-warlock", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Battle-hardened distributed locking using redis", | ||
@@ -5,0 +5,0 @@ "main": "lib/warlock.js", |
@@ -72,2 +72,11 @@ warlock | ||
}); | ||
// change a lock's ttl | ||
var key = 'touch-lock'; | ||
var ttl = 10000; | ||
var ttl2 = 20000; | ||
warlock.lock(key, ttl, function(err, unlock, id) { | ||
warlock.touch(key, id, ttl2, function(err) {}); | ||
}); | ||
``` | ||
@@ -74,0 +83,0 @@ |
@@ -12,3 +12,2 @@ const should = require('should'); | ||
(typeof unlock).should.equal('function'); | ||
done(); | ||
@@ -78,1 +77,35 @@ }); | ||
}); | ||
describe('touching a lock', function() { | ||
var key = 'touchlock' | ||
, lockId; | ||
it('sets lock and gets lock id', function(done) { | ||
warlock.lock(key, 1000, function(err, unlock, id) { | ||
should.not.exists(err); | ||
id.should.type("string"); | ||
lockId = id; | ||
done(); | ||
}); | ||
}); | ||
it('alters expiry of the lock', function(done) { | ||
redis.pttl(warlock.makeKey(key), function(err, ttl) { | ||
warlock.touch(key, lockId, 2000, function(err) { | ||
should.not.exist(err); | ||
redis.pttl(warlock.makeKey(key), function(err, ttl2) { | ||
(ttl2 > ttl).should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('unlocks', function(done) { | ||
warlock.unlock(key, lockId, function(err, result) { | ||
should.not.exists(err); | ||
result.should.equal(1); | ||
done(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
12606
12
198
86