Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom retry delay to Redlock #724

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pottery/redlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class Redlock(Scripts, Primitive):
'num_extensions',
'context_manager_blocking',
'context_manager_timeout',
'retry_delay',
'_uuid',
'_extension_num',
)
Expand All @@ -240,6 +241,7 @@ def __init__(self,
num_extensions: int = _NUM_EXTENSIONS,
context_manager_blocking: bool = True,
context_manager_timeout: float = -1,
retry_delay: float = _RETRY_DELAY,
) -> None:
'''Initialize a Redlock.

Expand All @@ -259,6 +261,8 @@ def __init__(self,
context_manager_timeout -- if context_manager_blocking, how long to
wait when acquiring before giving up and raising the
QuorumNotAchieved exception
retry_delay -- the upper bound of how long to block before attempting
to re-acquire the lock
'''
if not context_manager_blocking and context_manager_timeout != -1:
raise ValueError("can't specify a timeout for a non-blocking call")
Expand All @@ -272,6 +276,7 @@ def __init__(self,
self.num_extensions = num_extensions
self.context_manager_blocking = context_manager_blocking
self.context_manager_timeout = context_manager_timeout
self.retry_delay = retry_delay
self._uuid = ''
self._extension_num = 0

Expand Down Expand Up @@ -420,7 +425,7 @@ def acquire(self,
self.__log_time_enqueued(timer, acquired=True)
return True
enqueued = True
delay = random.uniform(0, self._RETRY_DELAY) # nosec
delay = random.uniform(0, self.retry_delay) # nosec
time.sleep(delay)
if enqueued: # pragma: no cover
self.__log_time_enqueued(timer, acquired=False)
Expand Down