Skip to content

Commit

Permalink
Bail out of Redlock.locked() on quorum (#390)
Browse files Browse the repository at this point in the history
Bail out of `Redlock.locked()` as soon as quorum is achieved.  This
makes the code both simpler and more efficient.
  • Loading branch information
brainix authored May 7, 2021
1 parent cac37e8 commit 0478f77
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pottery/redlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,13 @@ def locked(self) -> int:
>>> printer_lock_1.release()
'''
with ContextTimer() as timer, \
concurrent.futures.ThreadPoolExecutor() as executor:
with ContextTimer() as timer, BailOutExecutor() as executor:
futures = set()
for master in self.masters:
future = executor.submit(self.__acquired_master, master)
futures.add(future)

ttls = []
ttls, quorum = [], False
for future in concurrent.futures.as_completed(futures):
try:
ttls.append(future.result())
Expand All @@ -395,16 +394,18 @@ def locked(self) -> int:
self.__class__.__name__,
error.__class__.__name__,
)
else:
num_masters_acquired = sum(ttl > 0 for ttl in ttls)
quorum = num_masters_acquired > len(self.masters) // 2
if quorum:
break

num_masters_acquired = sum(ttl > 0 for ttl in ttls)
quorum = num_masters_acquired > len(self.masters) // 2
if quorum:
ttls = sorted(ttls, reverse=True)
validity_time = ttls[len(self.masters) // 2]
validity_time = min(ttl for ttl in ttls if ttl > 0)
validity_time -= round(timer.elapsed() + self.__drift())
return max(validity_time, 0)

return 0
return 0

__locked = locked

Expand Down

0 comments on commit 0478f77

Please sign in to comment.