Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDeveloper committed Apr 2, 2021
1 parent af859f6 commit e52581b
Show file tree
Hide file tree
Showing 10 changed files with 2,344 additions and 259 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
env:
commonjs: true
es2021: true
node: true
extends:
- airbnb-base
parserOptions:
ecmaVersion: 12
rules: {}
34 changes: 16 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ Battle-hardened distributed locking using redis.
## Usage

```javascript

var Warlock = require('node-redis-warlock');
var redis = require('redis');
const Warlock = require('node-redis-warlock');
const Redis = require('redis');

// Establish a redis client and pass it to warlock
var redis = redis.createClient();
var warlock = Warlock(redis);
const redis = Redis.createClient();
const warlock = Warlock(redis);

// Set a lock
var key = 'test-lock';
var ttl = 10000; // Lifetime of the lock
const key = 'test-lock';
const ttl = 10000; // Lifetime of the lock

warlock.lock(key, ttl, function(err, unlock){
warlock.lock(key, ttl, (err, unlock) => {
if (err) {
// Something went wrong and we weren't able to set a lock
return;
Expand All @@ -51,28 +50,27 @@ warlock.lock(key, ttl, function(err, unlock){
});

// set a lock optimistically
var key = 'opt-lock';
var ttl = 10000;
var maxAttempts = 4; // Max number of times to try setting the lock before erroring
var wait = 1000; // Time to wait before another attempt if lock already in place
warlock.optimistic(key, ttl, maxAttempts, wait, function(err, unlock) {});
const key = 'opt-lock';
const ttl = 10000;
const maxAttempts = 4; // Max number of times to try setting the lock before erroring
const wait = 1000; // Time to wait before another attempt if lock already in place
warlock.optimistic(key, ttl, maxAttempts, wait, (err, unlock) => {});

// unlock using the lock id
var key = 'test-lock-2';
var ttl = 10000;
var lockId;
let lockId;

warlock.lock(key, ttl, function(err, _, id) {
warlock.lock(key, ttl, (err, _, id) => {
lockId = id;
});

// each client who knows the lockId can release the lock
warlock.unlock(key, lockId, function(err, result) {
if(result == 1) {
warlock.unlock(key, lockId, (err, result) => {
if (result == 1) {
// unlocked successfully
}
});

```

## ProTips
Expand Down
5 changes: 3 additions & 2 deletions lib/del.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const path = require('path');

const _path = path.resolve(__dirname, './lua/parityDel.lua');
const src = require('fs').readFileSync(_path, { encoding: 'utf-8' });
const { createScript } = require('node-redis-script');

exports.createScript = redis => {
exports.createScript = (redis) => {
const opts = { redis };
return createScript(opts, src);
}
};
36 changes: 17 additions & 19 deletions lib/warlock.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
var crypto = require('crypto');
var UUID = require('uuid');

const UUID = require('uuid');
const { createScript } = require('./del');

module.exports = function(redis){
var warlock = {};
module.exports = function (redis) {
const warlock = {};

const parityDel = createScript(redis);

warlock.makeKey = function(key) {
return key + ':lock';
warlock.makeKey = function (key) {
return `${key}:lock`;
};

/**
Expand All @@ -18,34 +16,34 @@ module.exports = function(redis){
* @param {integer} ttl Time in milliseconds for the lock to live.
* @param {Function} cb
*/
warlock.lock = function(key, ttl, cb) {
cb = cb || function(){};
warlock.lock = function (key, ttl, cb) {
cb = cb || function () {};

if (typeof key !== 'string') {
return cb(new Error('lock key must be string'));
}

var id;
let id;
UUID.v1(null, (id = new Buffer(16)));
id = id.toString('base64');
redis.set(
warlock.makeKey(key), id,
'PX', ttl, 'NX',
function(err, lockSet) {
(err, lockSet) => {
if (err) return cb(err);

var unlock = warlock.unlock.bind(warlock, key, id);
let unlock = warlock.unlock.bind(warlock, key, id);
if (!lockSet) unlock = false;

return cb(err, unlock, id);
}
},
);

return key;
};

warlock.unlock = async (key, id, cb) => {
cb = cb || function(){};
cb = cb || function () {};

if (typeof key !== 'string') {
return cb(new Error('lock key must be string'));
Expand All @@ -64,17 +62,17 @@ module.exports = function(redis){
/**
* Set a lock optimistically (retries until reaching maxAttempts).
*/
warlock.optimistic = function(key, ttl, maxAttempts, wait, cb) {
var attempts = 0;
warlock.optimistic = function (key, ttl, maxAttempts, wait, cb) {
let attempts = 0;

var tryLock = function() {
var tryLock = function () {
attempts += 1;
warlock.lock(key, ttl, function(err, unlock) {
warlock.lock(key, ttl, (err, unlock) => {
if (err) return cb(err);

if (typeof unlock !== 'function') {
if (attempts >= maxAttempts) {
var e = new Error('unable to obtain lock');
const e = new Error('unable to obtain lock');
e.maxAttempts = maxAttempts;
e.key = key;
e.ttl = ttl;
Expand Down
Loading

0 comments on commit e52581b

Please sign in to comment.