mirror of
https://github.com/aljazceru/IngestRSS.git
synced 2025-12-18 14:34:26 +01:00
31 lines
1.2 KiB
Python
31 lines
1.2 KiB
Python
import time
|
|
from botocore.exceptions import ClientError
|
|
import logging
|
|
import os
|
|
logging.basicConfig(level=os.getenv('LOG_LEVEL', 'INFO'))
|
|
|
|
def retry_with_backoff(max_retries=20, initial_backoff=1, backoff_multiplier=4):
|
|
def decorator(func):
|
|
def wrapper(*args, **kwargs):
|
|
retries = 0
|
|
backoff = initial_backoff
|
|
|
|
while retries < max_retries:
|
|
try:
|
|
return func(*args, **kwargs)
|
|
except ClientError as e:
|
|
print(e)
|
|
if e.response['Error']['Code'] in ['ResourceConflictException', 'ResourceInUseException']:
|
|
if retries == max_retries - 1:
|
|
raise
|
|
wait_time = backoff * (2 ** retries)
|
|
logging.info(f"Encountered {e.response['Error']['Code']}. Retrying in {wait_time} seconds...")
|
|
time.sleep(wait_time)
|
|
retries += 1
|
|
backoff *= backoff_multiplier
|
|
else:
|
|
raise
|
|
raise Exception(f"Function failed after {max_retries} retries.")
|
|
|
|
return wrapper
|
|
return decorator |