def do_extend(self, additional_time):
pipe = self.redis.pipeline()
pipe.watch(self.name)
lock_value = pipe.get(self.name)
if lock_value != self.local.token:
raise LockError("Cannot extend a lock that's no longer owned")
expiration = pipe.pttl(self.name)
if expiration is None or expiration < 0:
# Redis evicted the lock key between the previous get() and now
# we'll handle this when we call pexpire()
expiration = 0
pipe.multi()
pipe.pexpire(self.name, expiration + int(additional_time * 1000))
try:
response = pipe.execute()
except WatchError:
# someone else acquired the lock
raise LockError("Cannot extend a lock that's no longer owned")
if not response[0]:
# pexpire returns False if the key doesn't exist
raise LockError("Cannot extend a lock that's no longer owned")
return True
评论列表
文章目录