-
Notifications
You must be signed in to change notification settings - Fork 48
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
Clear the lock only if it was created by the same process #29
Comments
Here is my implementation ; I didn't care for retries.
|
You could further optimise the lock clearance logic by moving it outside of node and into Redis itself. Redis allows you to use lua for such purposes. |
Tagging @rakeshpai In case he didn't get notified. |
Hey @rohitm. Sorry, been busy. Wouldn't this mean that that if the process crashes before it releases the lock, that's a deadlock situation since no one will now be able to acquire the lock? |
Right, So in the deadlock situation, you've set the lock to expire in 5 seconds by default. Line 15 in dcfa15f
After which other processes will be able to continue creating a lock. You should still be good. The recommendation to clear the lock based on the creator isn't novel or mine, its from redis. One can always alter the default 5 seconds to fit their need. |
redis-lock/index.js
Line 44 in dcfa15f
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.
I hope this helps. Cheerio!
The text was updated successfully, but these errors were encountered: