@@ -19,7 +19,7 @@ class Lock(object):
19
19
lua_reacquire = None
20
20
21
21
# KEYS[1] - lock name
22
- # ARGS [1] - token
22
+ # ARGV [1] - token
23
23
# return 1 if the lock was released, otherwise 0
24
24
LUA_RELEASE_SCRIPT = """
25
25
local token = redis.call('get', KEYS[1])
@@ -31,8 +31,10 @@ class Lock(object):
31
31
"""
32
32
33
33
# KEYS[1] - lock name
34
- # ARGS[1] - token
35
- # ARGS[2] - additional milliseconds
34
+ # ARGV[1] - token
35
+ # ARGV[2] - additional milliseconds
36
+ # ARGV[3] - "0" if the additional time should be added to the lock's
37
+ # existing ttl or "1" if the existing ttl should be replaced
36
38
# return 1 if the locks time was extended, otherwise 0
37
39
LUA_EXTEND_SCRIPT = """
38
40
local token = redis.call('get', KEYS[1])
@@ -46,13 +48,18 @@ class Lock(object):
46
48
if expiration < 0 then
47
49
return 0
48
50
end
49
- redis.call('pexpire', KEYS[1], expiration + ARGV[2])
51
+
52
+ local newttl = ARGV[2]
53
+ if ARGV[3] == "0" then
54
+ newttl = ARGV[2] + expiration
55
+ end
56
+ redis.call('pexpire', KEYS[1], newttl)
50
57
return 1
51
58
"""
52
59
53
60
# KEYS[1] - lock name
54
- # ARGS [1] - token
55
- # ARGS [2] - milliseconds
61
+ # ARGV [1] - token
62
+ # ARGV [2] - milliseconds
56
63
# return 1 if the locks time was reacquired, otherwise 0
57
64
LUA_REACQUIRE_SCRIPT = """
58
65
local token = redis.call('get', KEYS[1])
@@ -231,26 +238,39 @@ def do_release(self, expected_token):
231
238
raise LockNotOwnedError ("Cannot release a lock"
232
239
" that's no longer owned" )
233
240
234
- def extend (self , additional_time ):
241
+ def extend (self , additional_time , replace_ttl = False ):
235
242
"""
236
243
Adds more time to an already acquired lock.
237
244
238
245
``additional_time`` can be specified as an integer or a float, both
239
246
representing the number of seconds to add.
247
+
248
+ ``replace_ttl`` if False (the default), add `additional_time` to
249
+ the lock's existing ttl. If True, replace the lock's ttl with
250
+ `additional_time`.
240
251
"""
241
252
if self .local .token is None :
242
253
raise LockError ("Cannot extend an unlocked lock" )
243
254
if self .timeout is None :
244
255
raise LockError ("Cannot extend a lock with no timeout" )
245
- return self .do_extend (additional_time )
256
+ return self .do_extend (additional_time , replace_ttl )
246
257
247
- def do_extend (self , additional_time ):
258
+ def do_extend (self , additional_time , replace_ttl ):
248
259
additional_time = int (additional_time * 1000 )
249
- if not bool (self .lua_extend (keys = [self .name ],
250
- args = [self .local .token , additional_time ],
251
- client = self .redis )):
252
- raise LockNotOwnedError ("Cannot extend a lock that's"
253
- " no longer owned" )
260
+ if not bool (
261
+ self .lua_extend (
262
+ keys = [self .name ],
263
+ args = [
264
+ self .local .token ,
265
+ additional_time ,
266
+ replace_ttl and "1" or "0"
267
+ ],
268
+ client = self .redis ,
269
+ )
270
+ ):
271
+ raise LockNotOwnedError (
272
+ "Cannot extend a lock that's" " no longer owned"
273
+ )
254
274
return True
255
275
256
276
def reacquire (self ):
0 commit comments