Skip to content

Commit ccd78af

Browse files
ENH: Add exponential backoff decorator to tools.py
1 parent a011c5a commit ccd78af

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

rocketpy/tools.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import functools
12
import importlib
23
import importlib.metadata
34
import re
5+
import time
46
from bisect import bisect_left
57

68
import numpy as np
@@ -382,6 +384,25 @@ def check_requirement_version(module_name, version):
382384
return True
383385

384386

387+
def exponential_backoff(max_attempts, base_delay=1, max_delay=60):
388+
def decorator(func):
389+
@functools.wraps(func)
390+
def wrapper(*args, **kwargs):
391+
delay = base_delay
392+
for i in range(max_attempts):
393+
try:
394+
return func(*args, **kwargs)
395+
except Exception as e:
396+
if i == max_attempts - 1:
397+
raise e from None
398+
delay = min(delay * 2, max_delay)
399+
time.sleep(delay)
400+
401+
return wrapper
402+
403+
return decorator
404+
405+
385406
def parallel_axis_theorem_from_com(com_inertia_moment, mass, distance):
386407
"""Calculates the moment of inertia of a object relative to a new axis using
387408
the parallel axis theorem. The new axis is parallel to and at a distance

0 commit comments

Comments
 (0)