Skip to content

Commit

Permalink
swap scripty
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDeveloper committed Apr 2, 2021
1 parent 13765ee commit af859f6
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 15 deletions.
13 changes: 13 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
redis:
image: redis:3.2-alpine
ports:
- 6386:6379
container_name: warlock-redis

monitor:
image: redis:3.2-alpine
links:
- redis
entrypoint: "/bin/sh"
command: >
"-c" "redis-cli -h warlock-redis -p 6379 monitor"
9 changes: 9 additions & 0 deletions lib/del.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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 => {
const opts = { redis };
return createScript(opts, src);
}
24 changes: 12 additions & 12 deletions lib/warlock.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
var crypto = require('crypto');
var Scripty = require('node-redis-scripty');
var UUID = require('uuid');

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

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

var scripty = new Scripty(redis);
const parityDel = createScript(redis);

warlock.makeKey = function(key) {
return key + ':lock';
Expand Down Expand Up @@ -43,22 +44,21 @@ module.exports = function(redis){
return key;
};

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

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

scripty.loadScriptFile(
'parityDel',
__dirname + '/lua/parityDel.lua',
function(err, parityDel){
if (err) return cb(err);

return parityDel.run(1, warlock.makeKey(key), id, cb);
}
);
const numKeys = 1;
const _key = warlock.makeKey(key);
try {
const result = await parityDel(numKeys, _key, id);
cb(null, result);
} catch (e) {
cb(e);
}
};

/**
Expand Down
247 changes: 247 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"description": "Battle-hardened distributed locking using redis",
"main": "lib/warlock.js",
"scripts": {
"test": "mocha -R list ./test/warlock",
"pretest": "npm run start-redis",
"posttest": "npm run cleanup",
"start-redis": "docker-compose up -d redis",
"test": "mocha ./test/warlock",
"cleanup": "docker-compose stop && docker-compose rm -f",
"bench": "mocha -R list ./test/bench"
},
"repository": {
Expand All @@ -23,6 +27,7 @@
},
"homepage": "https://github.com/thedeveloper/warlock",
"dependencies": {
"node-redis-script": "^2.0.1",
"node-redis-scripty": "0.0.5",
"uuid": "^2.0.1"
},
Expand Down
6 changes: 4 additions & 2 deletions test/warlock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var should = require('should');
var redis = require('./setup/redisConnection');

var Redis = require('redis');
var redis = Redis.createClient({ port: 6386 });

var warlock = require('../lib/warlock')(redis);
require('./setup/redisFlush');

describe('locking', function() {
it('sets lock', function (done) {
Expand Down

0 comments on commit af859f6

Please sign in to comment.