Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
"use strict";

var util = require('util');
var defaultTimeout = 5000;
var promisify = util.promisify || function(x) { return x; };
const util = require('util');
const defaultTimeout = 5000;
const promisify = util.promisify || function(x) { return x; };

function acquireLock(client, lockName, timeout, retryDelay, onLockAcquired) {
function retry() {
setTimeout(function() {
/* this is where we recursively re-call ourself */
setTimeout(() => {
acquireLock(client, lockName, timeout, retryDelay, onLockAcquired);
}, retryDelay);
}

var lockTimeoutValue = (Date.now() + timeout + 1);
client.set(lockName, lockTimeoutValue, 'PX', timeout, 'NX', function(err, result) {
/* `lockTimeoutValue` is the value after which the lock is automatically released. wathever the use? */
const lockTimeoutValue = (Date.now() + timeout + /* just in case is 0 we add 1 ms */ 1);
client.set(lockName, lockTimeoutValue, 'PX', timeout, 'NX', (err, result) => {
if(err || result === null) return retry();
onLockAcquired(lockTimeoutValue);
});
}

module.exports = function(client, retryDelay) {
module.exports = (client, retryDelay) => {
if(!(client && client.setnx)) {
throw new Error("You must specify a client instance of http://github.com/mranney/node_redis");
}
Expand Down