Skip to content

Commit

Permalink
refactor: Use native Lock timeout instead of reimplementing
Browse files Browse the repository at this point in the history
  • Loading branch information
a-ungurianu committed Oct 24, 2022
1 parent a43ef2b commit 0cf2ba0
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions kazoo/recipe/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def __init__(self, client, path, identifier=None, extra_lock_patterns=()):
self._retry = KazooRetry(
max_tries=None, sleep_func=client.handler.sleep_func
)
self._lock = client.handler.lock_object()
self._thread_lock = client.handler.lock_object()

def _ensure_path(self):
self.client.ensure_path(self.path)
Expand Down Expand Up @@ -174,27 +174,17 @@ def acquire(self, blocking=True, timeout=None, ephemeral=True):
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:
thread_locked = self._thread_lock.acquire(
blocking=blocking, timeout=timeout
)
if not thread_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
Expand All @@ -220,7 +210,7 @@ def _acquire_lock():
self._best_effort_cleanup()
return gotten
finally:
self._lock.release()
self._thread_lock.release()

def _watch_session(self, state):
self.wake_event.set()
Expand Down

0 comments on commit 0cf2ba0

Please sign in to comment.