Skip to content

Clear the lock only if it was created by the same process #29

Description

@rohitm

if(lockTimeoutValue > Date.now()) {

I've been testing your lock coupled with an expressjs server and the lock doesn't work if you pass concurrent requests to the webserver. If you look at the correct implementation prescribed on the Redis website, you'll find that lock should be cleared only by the process or routine that acquired the lock in the first place.

I therefore landed up rewriting the lock / unlock logic to suit my need. I think your code will "better" guarantee mutual exclusion if you don't rely on timestamps for lock acquisition and clearance. Instead use a process ID or a randomly generated UUID.

// Set lock by randomUUID on line 15
// TODO : Generate randomUUID and save it in somewhere in the lock's context
client.set(lockName, randomUUID, 'PX', timeout, 'NX', function(err, result) { ...


// Clear lock by verifying the lock's creator on line 44
// TODO : Figure out who acquired the lock from redis 'GET lockName'
if(lockSetByUUID === randomUUID) {
	client.del(lockName, done);
...	

I hope this helps. Cheerio!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions