diff options
author | Alex Ungurianu <alex.ungurianu@protonmail.com> | 2022-10-30 03:05:22 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-29 23:05:22 -0400 |
commit | 51b875ae7186830733f1cd41c2e034cf37a8cbfe (patch) | |
tree | d3a6bb219153a53a4f5d17e974a67f9226db628e | |
parent | a43ef2bfb11691ae3f490c3a2eb3319e7c4f6222 (diff) | |
download | kazoo-51b875ae7186830733f1cd41c2e034cf37a8cbfe.tar.gz |
recipe(lock): Use native Lock timeout instead of reimplementing (#676)
Closes #605
-rw-r--r-- | kazoo/recipe/lock.py | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/kazoo/recipe/lock.py b/kazoo/recipe/lock.py index d891d63..8959964 100644 --- a/kazoo/recipe/lock.py +++ b/kazoo/recipe/lock.py @@ -134,7 +134,7 @@ class Lock(object): self._retry = KazooRetry( max_tries=None, sleep_func=client.handler.sleep_func ) - self._lock = client.handler.lock_object() + self._acquire_method_lock = client.handler.lock_object() def _ensure_path(self): self.client.ensure_path(self.path) @@ -174,27 +174,17 @@ class Lock(object): The ephemeral option. """ - def _acquire_lock(): - got_it = self._lock.acquire(False) - if not got_it: - raise ForceRetryError() - return True - retry = self._retry.copy() retry.deadline = timeout # Ensure we are locked so that we avoid multiple threads in # this acquistion routine at the same time... - locked = self._lock.acquire(False) - if not locked and not blocking: + method_locked = self._acquire_method_lock.acquire( + blocking=blocking, timeout=timeout if timeout is not None else -1 + ) + if not method_locked: return False - if not locked: - # Lock acquire doesn't take a timeout, so simulate it... - # XXX: This is not true in Py3 >= 3.2 - try: - locked = retry(_acquire_lock) - except RetryFailedError: - return False + already_acquired = self.is_acquired try: gotten = False @@ -220,7 +210,7 @@ class Lock(object): self._best_effort_cleanup() return gotten finally: - self._lock.release() + self._acquire_method_lock.release() def _watch_session(self, state): self.wake_event.set() |