Skip to content

Commit

Permalink
Use ARGV for Redis lua scripts
Browse files Browse the repository at this point in the history
Closes #31
  • Loading branch information
judahrand committed Oct 6, 2022
1 parent 970aa5a commit 7df7056
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ Development Version
* [FEAT] Install backend specific dependencies with extras #59_
* [FEAT] Add `.renew()` method to all backends #61_

* [BUGFIX] Use `ARGV` in Redis Lua scripts to add RedisCluster compatibility #31_
* [BUGFIX] `redis>=2.10.6` client won't work with `sherlock 0.3.1` #32_
* [BUGFIX] `timeout=0` doesn't work as expected with `RedisLock` #60_

.. _#31: https://github.com/vaidik/sherlock/issues/31
.. _#32: https://github.com/vaidik/sherlock/issues/32
.. _#59: https://github.com/py-sherlock/sherlock/pull/59
.. _#60: https://github.com/py-sherlock/sherlock/pull/60
Expand Down
24 changes: 14 additions & 10 deletions sherlock/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,16 +412,16 @@ class RedisLock(BaseLock):
"""

_acquire_script = """
local result = redis.call('SETNX', KEYS[1], KEYS[2])
if result == 1 and KEYS[3] ~= -1 then
redis.call('EXPIRE', KEYS[1], KEYS[3])
local result = redis.call('SETNX', KEYS[1], ARGV[1])
if result == 1 and ARGV[2] ~= -1 then
redis.call('EXPIRE', KEYS[1], ARGV[2])
end
return result
"""

_release_script = """
local result = 0
if redis.call('GET', KEYS[1]) == KEYS[2] then
if redis.call('GET', KEYS[1]) == ARGV[1] then
redis.call('DEL', KEYS[1])
result = 1
end
Expand All @@ -430,9 +430,9 @@ class RedisLock(BaseLock):

_renew_script = """
local result = 0
if redis.call('GET', KEYS[1]) == KEYS[2] then
if KEYS[3] ~= -1 then
redis.call('EXPIRE', KEYS[1], KEYS[3])
if redis.call('GET', KEYS[1]) == ARGV[1] then
if ARGV[2] ~= -1 then
redis.call('EXPIRE', KEYS[1], ARGV[2])
else
redis.call('PERSIST', KEYS[1])
end
Expand Down Expand Up @@ -485,7 +485,7 @@ def _acquire(self):
expire = -1
else:
expire = self.expire
if self._acquire_func(keys=[self._key_name, owner, expire]) != 1:
if self._acquire_func(keys=[self._key_name], args=[owner, expire]) != 1:
return False
self._owner = owner
return True
Expand All @@ -494,7 +494,7 @@ def _release(self):
if self._owner is None:
raise LockException("Lock was not set by this process.")

if self._release_func(keys=[self._key_name, self._owner]) != 1:
if self._release_func(keys=[self._key_name], args=[self._owner]) != 1:
raise LockException(
"Lock could not be released because it was "
"not acquired by this instance."
Expand All @@ -506,7 +506,10 @@ def _renew(self) -> bool:
if self._owner is None:
raise LockException("Lock was not set by this process.")

if self._release_func(keys=[self._key_name, self._owner, self.expire]) != 1:
if (
self._release_func(keys=[self._key_name], args=[self._owner, self.expire])
!= 1
):
return False
return True

Expand Down Expand Up @@ -1209,6 +1212,7 @@ def _release(self) -> None:

if self._owner == data["owner"]:
self._data_file.unlink()
pathlib.Path(self._lock_file.lock_file).unlink()

def _renew(self) -> bool:
if self._owner is None:
Expand Down

0 comments on commit 7df7056

Please sign in to comment.