utils.py 861 B

123456789101112131415161718192021222324252627282930
  1. import time
  2. from botocore.exceptions import ClientError
  3. def remove_none(d: dict):
  4. return {k: v for k, v in d.items() if v is not None and v != ""}
  5. def retry_wrapper(fn, retry_wait_seconds=2, retry_factor=2, max_retries=5):
  6. """Exponential back-off retry wrapper for ClientError exceptions"""
  7. def wrapper(*args, **kwargs):
  8. retry_current = 0
  9. last_error = None
  10. while retry_current <= max_retries:
  11. try:
  12. return fn(*args, **kwargs)
  13. except ClientError as e:
  14. nonlocal retry_wait_seconds
  15. if retry_current == max_retries:
  16. break
  17. last_error = e
  18. retry_current += 1
  19. time.sleep(retry_wait_seconds)
  20. retry_wait_seconds *= retry_factor
  21. raise last_error
  22. return wrapper