Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript #28

Draft
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ env:
commonjs: true
es2021: true
node: true
mocha: true
extends:
- airbnb-base
parserOptions:
Expand Down
15 changes: 1 addition & 14 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

npm-debug.log
node_modules
dist
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ Battle-hardened distributed locking using redis.

## Usage

```js
const { createClient } = require('redis');
const redis = createClient();

const opts = { redis };
const warlock = new Warlock(opts);

const unlock = await warlock.lock(key, ttl);
await unlock();
```

```javascript
const Warlock = require('node-redis-warlock');
const Redis = require('redis');
Expand Down Expand Up @@ -83,3 +94,12 @@ warlock.lock(key, ttl, function(err, unlock, id) {
## ProTips

* Read my [Distributed locks using Redis](https://engineering.gosquared.com/distributed-locks-using-redis) article and Redis' author's [A proposal for more reliable locks using Redis](http://antirez.com/news/77) to learn more about the theory of distributed locks using Redis.


# Test
```bash
npm run start-redis
npm run watch
npx mocha --exit dist/test/warlock
npm run cleanup
```
10 changes: 3 additions & 7 deletions lib/lua/parityRelock.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
--
-- Extend the key if content is equal
-- This will extend the lock on the
--
-- KEYS[1] - key
-- KEYS[2] - content
-- KEYS[3] - lock id
local key = KEYS[1]
local ttl = KEYS[2]
local id = KEYS[3]
local ttl = ARGV[1]
local id = ARGV[2]

local value = redis.call('GET', key)

if value == id then
return redis.call('SET', key, id, 'PX', ttl, 'XX');
else
return 0
end
end
88 changes: 88 additions & 0 deletions lib/warlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { RedisClient } from "redis";
import * as uuid from "uuid";
import AsyncRedis from 'async-redis';
import { createScript } from 'node-redis-script';
import Redis from 'redis';

import { readFileSync } from 'fs';
import { resolve as resolvePath } from 'path';

function createParityDel(redis: RedisClient) {
const path = resolvePath(__dirname, '../../lib/lua/parityDel.lua');
const parityDelSrc = readFileSync(path, 'utf8');
const opts = { redis };
return createScript(opts, parityDelSrc);
}

function createParityRelock(redis: RedisClient) {
const path = resolvePath(__dirname, '../../lib/lua/parityRelock.lua');
const parityRelockSrc = readFileSync(path, 'utf8');
const opts = { redis };
return createScript(opts, parityRelockSrc);
}

function wait(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
interface WarlockOpts {
redis?: RedisClient
}
export class Warlock {
redis: RedisClient
parityDel: any;
parityRelock: any;

constructor(opts: WarlockOpts = {}) {
let redis = opts.redis;
if (!redis) redis = Redis.createClient();

this.parityDel = createParityDel(redis);
this.parityRelock = createParityRelock(redis);

this.redis = AsyncRedis.decorate(redis);
}

async lock(key: string, ttlMs: number) {
const id = uuid.v4();
const _key = `${key}:lock`;
const result = await this.redis.set(_key, id, 'PX', ttlMs, 'NX');
if (result) {
const unlock = () => this.unlock(key, id);
return unlock;
}
return false;
}

async unlock(key: string, id: string): Promise<0|1> {
const numKeys = 1;
const _key = `${key}:lock`;
const result = await this.parityDel(numKeys, _key, id);
return result;
}

async optimistic(
key: string,
ttlMs: number,
maxAttempts: number,
waitMs: number
) {
let attempts = 0;
let unlock;

while (!unlock && attempts < maxAttempts) {
if (attempts > 0) await wait(waitMs);
unlock = await this.lock(key, ttlMs);
attempts += 1;
}

return unlock;
}

async touch(key: string, id: string, ttlMs: number) {
const _key = `${key}:lock`;
const result = await this.parityRelock(1, _key, ttlMs, id);
return result;
}
}
Loading