-
Notifications
You must be signed in to change notification settings - Fork 0
/
backoff.py
40 lines (36 loc) · 1.42 KB
/
backoff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import random
from time import sleep
from botocore.exceptions import ClientError
def backoff_with_jitter(base, cap, retries):
backoff = min(cap, base * 2 ** retries)
jittered_backoff = backoff / 2 + random.uniform(0.0, backoff / 2)
print(f"backing off {jittered_backoff} seconds.")
sleep(jittered_backoff)
def aws_client_backoff(
base: float = 0.5,
cap: int = 120,
max_retry: int = 6
):
def decorator(func):
def wrapper(*args, **kwargs):
retry_count = 0
done = False
while not done and retry_count <= max_retry:
try:
if retry_count:
print("retrying...")
func(*args, **kwargs)
done = True
except ClientError as exception:
retry_count += 1
if exception.response['Error']['Code'] == "ThrottlingException":
print(f"Throttling occurred, backing off...")
backoff_with_jitter(base, cap, retry_count)
else:
print(f"exception.response['Error']['Code'], occured...")
if not done:
raise ClientError(f"Unable to process {func.__name__} with {max_retry} retries\n"
f"args:{args}, \n"
f"kwargs:{kwargs}")
return wrapper
return decorator