Skip to content

Commit

Permalink
Add utiloori.func.retry_exponential_backoff()
Browse files Browse the repository at this point in the history
  • Loading branch information
uogbuji committed Nov 22, 2024
1 parent 1c931be commit 4475b94
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions pylib/func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import time
import random

def retry_exponential_backoff(
func,
initial_delay: float = 1,
exponential_base: float = 2,
jitter: bool = True,
max_retries: int = 10,
errors: tuple = None,
retry_if_retval = None,
log_errors = None
):
'''
Retry function call with exponential backoff
'''

def wrapper(*args, **kwargs):
# Initialize variables
num_retries = 0
delay = initial_delay

# Loop until a successful response or max_retries is hit or an exception is raised
while True:
try:
retval = func(*args, **kwargs)
if retval in retry_if_retval:
err = ValueError(f'Unwanted return value {retval}')
else:
return retval

# Retry on specific errors
except errors as e:
err = e

# Raise exceptions for any errors not specified
except Exception as e:
raise e

if log_errors:
log_errors(err)

num_retries += 1

if num_retries > max_retries:
# Max retries has been reached
raise RuntimeError(f'Maximum number of retries ({max_retries}) exceeded.')

# Backoff: Increment delay
delay *= exponential_base * (1 + jitter * random.random())

# Sleep for the delay
time.sleep(delay)

return wrapper

0 comments on commit 4475b94

Please sign in to comment.