Skip to content

Commit

Permalink
feat: Calculate jitter range once, and some safeguards against negati…
Browse files Browse the repository at this point in the history
…ve values
  • Loading branch information
krishnasism committed Mar 17, 2024
1 parent de16fdf commit 3cb5439
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions retry_later/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async def wrapper(
**kwargs: KWARGS_T,
) -> None:
retries = 0
jitter_range = range(0, max(0, max_jitter) + 1)
while retries < max_retries:
try:
if inspect.iscoroutinefunction(func):
Expand All @@ -55,8 +56,8 @@ async def wrapper(
if retries >= max_retries:
raise e
backoff_factor = backoff**retries if exponential_backoff else backoff
delay = retry_interval * backoff_factor + random.choice(range(0, max_jitter + 1))
delay = delay if max_delay == -1 else min(max_delay, delay)
delay = retry_interval * backoff_factor + random.choice(jitter_range)
delay = delay if max_delay < 0 else min(max_delay, delay)
logging.error(f"[retry later] Retrying in {delay}s due to {e}")
await asyncio.sleep(delay)

Expand Down

0 comments on commit 3cb5439

Please sign in to comment.