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

Replace redis by ioredis #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 5 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ async function acquireLock (client, lockName, timeout, retryDelay, onLockAcquire

const lockTimeoutValue = Date.now() + timeout + 1;
try {
const result = await client.set(lockName, lockTimeoutValue, {
PX: timeout,
NX: true
});
const result = await client.set(lockName, lockTimeoutValue, "NX")
if (result === null) {
throw new Error("Lock failed");
}
await client.set(lockName, lockTimeoutValue, "PX", timeout)
if (result === null) {
throw new Error("Lock failed");
}
Expand All @@ -26,9 +27,6 @@ async function acquireLock (client, lockName, timeout, retryDelay, onLockAcquire
}

function redisLock (client, retryDelay = DEFAULT_RETRY_DELAY) {
if(!(client && client.set && "v4" in client)) {
throw new Error("You must specify a v4 client instance of https://github.com/redis/node-redis");
}
async function lock (lockName, timeout = DEFAULT_TIMEOUT) {
return new Promise(resolve => {
if (!lockName) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dependencies": {},
"devDependencies": {
"mocha": "4.x",
"redis": "4.x",
"ioredis": "5.x",
"should": "^13.x"
},
"scripts": {
Expand Down
18 changes: 13 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const should = require("should"),
redisClient = require("redis").createClient(),
lock = require("../index")(redisClient)
const should = require("should")
const Redis = require("ioredis")
const redisClient = new Redis();
const lock = require("../index")(redisClient)


const delay = (fn, ms) => new Promise(
res => setTimeout(async () => {
Expand All @@ -11,7 +13,12 @@ const delay = (fn, ms) => new Promise(

describe("redis-lock", function() {
before(async () => {
await redisClient.connect();
try {
await redisClient.connect();
}
catch (err) {
// Ignore if redis have connected
}
})

after(async () => {
Expand Down Expand Up @@ -51,7 +58,8 @@ describe("redis-lock", function() {

const completed = await lock("testLock");
// This should be called after 300 ms
(new Date() - start).should.be.above(300);
const timeout = (new Date()) - start
timeout.should.be.above(300);
await completed();
});
});