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

use random string tokens in LuaLock #411

Closed
wants to merge 8 commits into from

Conversation

chillipino
Copy link
Contributor

this pull request is based on #405 from @natict addressing the issues from comments. since it looks like he hasn't had time to respond to those comments, i made changes based on his implementation; apologies to the community if doing this is frowned upon or if there's a better way (his changeset is in this pull request, so merging this would obviate #405). i saw the comment in #408 saying #405 would get merged in, but i haven't seen a commit for that yet, feel free to close this if you're already done.

the main thing this patch changes from @natict's implementation is to store a random token rather than the floating point value of the client machine's clock in string form, as Lock currently does. one invariant goes away as a side effect - after a release, token in the LuaLock instance keeps its previous value, rather than being set to None as Lock does to acquired_until. i couldn't find a good way to keep that behavior since the script is effectively atomic - adding a pipeline to release() would probably work, but i chose to keep the code simple for now.

feedback welcome on everything, but i'd specifically like comments about the redis.client.generate_random_token() function - there may be better/more desirable ways to do this.

i didn't touch any of the context manager stuff, so the changes in #408 would need to be duplicated in LuaLock.

each lock implementation has its own test suite now, and both are always run. i didn't do anything special if the redis server being used is pre-2.6 - LuaLock tests will always fail in that case. i'm not sure what you target for automated tests, so this may need some touchups.

for both Lock and LuaLock, it appears to be possible to incorrectly release a lock that has a timeout when sharing a lock instance within a single process. i need to set up a test case to confirm this, and if it does happen, i'll open a separate issue to propose a solution.

Nati CT and others added 8 commits November 28, 2013 11:21
* add redis.client.generate_random_token(), default token generator function
* store the token returned by the token_generator function provided to LuaLock.__init__() instead of the floating-point representation of the timeout value
* StrictRedis.lock() chooses to use LuaLock over Lock if the server accepts the script registration (pre-2.6 servers will return an error)
@andymccurdy
Copy link
Contributor

@chillipino Thanks for all your work on this. I've pulled in a bunch of your code and merged it with a few other ideas from other issues too. It'll go out with 2.10.

@andymccurdy
Copy link
Contributor

And 2.10 is out. Again, thanks for this and sorry it took me so long to integrate it all together.

@andymccurdy andymccurdy closed this Jun 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants