expired or released lease by calling renew. Go create a new lease
object at that point. Finally, note that when you renew the lease
it will evaluate to False briefly as it is reobtained.
-
"""
def __init__(
self.utcnow = utcnow
def release(self) -> bool:
- """Release the lease, if it's presently being held."""
+ """Release the lease, if it's presently being held.
+
+ Returns:
+ True if the lease was successfully released,
+ False otherwise.
+ """
self.client.ensure_path(self.path)
holder_path = self.path + "/lease_holder"
lock = self.client.Lock(self.path, self.identifier)
return False
def try_renew(self, duration: datetime.timedelta) -> bool:
+ """Attempt to renew a lease that is currently held. Note that
+ this will cause self to evaluate to False briefly as the lease
+ is renewed.
+
+ Args:
+ duration: the amount of additional time to add to the
+ current lease expiration.
+
+ Returns:
+ True if the lease was successfully renewed,
+ False otherwise.
+ """
+
if not self.obtained:
return False
self.obtained = False
return False
def __bool__(self):
+ """Note that this implementation differs from that of the base
+ class in that it probes zookeeper to ensure that the lease is
+ not yet expired and is therefore more expensive.
+ """
+
if not self.obtained:
return False
lock = self.client.Lock(self.path, self.identifier)