Skip to content

Commit

Permalink
rename arg name keep_remaining -> add_to_exist_ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
laixintao committed Mar 13, 2020
1 parent 38f4b29 commit 9484e99
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions CHANGES
Expand Up @@ -22,8 +22,8 @@
a roundtrip to the server by not having to call UNWATCH within
Pipeline.reset(). Thanks @nickgaya, #1299/#1302
* Add the KEEPTTL option for the SET command. Thanks @laixintao #1304/#1280
* Lock.extend now has a new option: keep_remaining, default to False, which
is identical with the old Lock.extend; When set to True, extend will set
* Lock.extend now has a new option: add_to_existing_ttl, default to True, which
is identical with the old Lock.extend; When set to False, extend will set
lock's ttl exactly the additional time.
* 3.4.1
* Move the username argument in the Redis and Connection classes to the
Expand Down
21 changes: 12 additions & 9 deletions redis/lock.py
Expand Up @@ -16,7 +16,6 @@ class Lock(object):

lua_release = None
lua_extend = None
lua_extend_to = None
lua_reacquire = None

# KEYS[1] - lock name
Expand Down Expand Up @@ -238,37 +237,41 @@ def do_release(self, expected_token):
raise LockNotOwnedError("Cannot release a lock"
" that's no longer owned")

def extend(self, additional_time, keep_remaining=True):
def extend(self, additional_time, add_to_existing_ttl=True):
"""
Adds more time to an already acquired lock.
``additional_time`` can be specified as an integer or a float, both
representing the number of seconds to add.
``keep_remaining`` indicates weather to keep the current left ttl.
``add_to_existing_ttl`` indicates weather to keep the current left ttl.
E.g. If current ``ttl name`` is 2, ``extend(name, 10)`` will result
``ttl name = 12``, and ``extend(name, 10, keep_remaining=False)`` will
result ``ttl name=10``.
``ttl name = 12``, and ``extend(name, 10, add_to_existing_ttl=False)``
will result ``ttl name=10``.
"""
if self.local.token is None:
raise LockError("Cannot extend an unlocked lock")
if self.timeout is None:
raise LockError("Cannot extend a lock with no timeout")
return self.do_extend(additional_time, keep_remaining)
return self.do_extend(additional_time, add_to_existing_ttl)

def do_extend(self, additional_time, keep_remaining):
def do_extend(self, additional_time, add_to_existing_ttl):
additional_time = int(additional_time * 1000)
# Only false and nil in lua consider as false, but there is no boolean
# type in redis so we can only send a string as argv[3] to redis
if keep_remaining:
if add_to_existing_ttl:
add_to_existing_ttl_arg = "yes"
else:
add_to_existing_ttl_arg = "no"

if not bool(
self.lua_extend(
keys=[self.name],
args=[self.local.token, additional_time, add_to_existing_ttl_arg],
args=[
self.local.token,
additional_time,
add_to_existing_ttl_arg
],
client=self.redis,
)
):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_lock.py
Expand Up @@ -153,7 +153,7 @@ def test_extend_lock_without_left_ttl(self, r):
lock = self.get_lock(r, 'foo', timeout=10)
assert lock.acquire(blocking=False)
assert 8000 < r.pttl('foo') <= 10000
assert lock.extend(10, keep_remaining=False)
assert lock.extend(10, add_to_existing_ttl=False)
assert 8000 < r.pttl('foo') <= 10000
lock.release()

Expand Down

0 comments on commit 9484e99

Please sign in to comment.