2020-12-18 06:36:05 -06:00
|
|
|
import abc
|
|
|
|
|
|
|
|
import time
|
2020-12-18 07:27:52 -06:00
|
|
|
import random
|
2020-12-18 06:36:05 -06:00
|
|
|
|
|
|
|
|
|
|
|
class RetryPolicy(abc.ABC):
|
|
|
|
@abc.abstractmethod
|
2023-07-12 04:42:17 -05:00
|
|
|
def sleep(self, retry_num: int) -> None:
|
2020-12-18 06:36:05 -06:00
|
|
|
"""
|
|
|
|
How long to sleep in milliseconds.
|
|
|
|
:param retry_num: the number of retry (starting from zero)
|
|
|
|
"""
|
|
|
|
assert retry_num >= 0
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2023-07-12 04:42:17 -05:00
|
|
|
def time_out_message(self) -> str:
|
2020-12-18 06:36:05 -06:00
|
|
|
"""
|
|
|
|
Generate a error message for user on time out.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2023-07-12 04:42:17 -05:00
|
|
|
def num_retries(self) -> int:
|
2020-12-18 06:36:05 -06:00
|
|
|
"""
|
|
|
|
Max number retries.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class FixedRetryPolicy(RetryPolicy):
|
2023-07-12 04:42:17 -05:00
|
|
|
def __init__(self, sleep_time: int = 1000, max_num_retries: int = 10):
|
2020-12-18 06:36:05 -06:00
|
|
|
"""
|
|
|
|
Create a fixed time retry policy.
|
|
|
|
:param sleep_time: time to sleep in milliseconds.
|
|
|
|
:param max_num_retries: max number of retries.
|
|
|
|
"""
|
2023-07-12 04:42:17 -05:00
|
|
|
self.sleep_time: int = sleep_time
|
|
|
|
self.max_num_retries: int = max_num_retries
|
2020-12-18 06:36:05 -06:00
|
|
|
|
2023-07-12 04:42:17 -05:00
|
|
|
def sleep(self, retry_num: int) -> None:
|
2020-12-18 06:36:05 -06:00
|
|
|
time.sleep(self.sleep_time / 1000)
|
|
|
|
|
2023-07-12 04:42:17 -05:00
|
|
|
def time_out_message(self) -> str:
|
2020-12-18 06:36:05 -06:00
|
|
|
return "Tried {} times with {} milliseconds apart.".format(
|
|
|
|
self.max_num_retries, self.sleep_time
|
|
|
|
)
|
|
|
|
|
2023-07-12 04:42:17 -05:00
|
|
|
def num_retries(self) -> int:
|
2020-12-18 06:36:05 -06:00
|
|
|
return self.max_num_retries
|
2020-12-18 07:27:52 -06:00
|
|
|
|
|
|
|
|
|
|
|
class ExponentialBackoffRetryPolicy(RetryPolicy):
|
2023-07-12 04:42:17 -05:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
min_backoff: int = 200,
|
|
|
|
max_backoff: int = 10000,
|
|
|
|
max_num_retries: int = 20,
|
|
|
|
):
|
2020-12-18 07:27:52 -06:00
|
|
|
"""
|
|
|
|
Create a truncated exponential backoff policy.
|
|
|
|
See: https://en.wikipedia.org/wiki/Exponential_backoff
|
|
|
|
:param min_backoff: minimum time to sleep in milliseconds.
|
|
|
|
:param max_backoff: maximum time to sleep in milliseconds.
|
|
|
|
:param max_num_retries: max number of retries.
|
|
|
|
"""
|
2023-07-12 04:42:17 -05:00
|
|
|
self.min_backoff: int = min_backoff
|
|
|
|
self.max_backoff: int = max_backoff
|
|
|
|
self.max_num_retries: int = max_num_retries
|
|
|
|
self.multiplier: int = 2
|
2020-12-18 07:27:52 -06:00
|
|
|
|
2023-07-12 04:42:17 -05:00
|
|
|
def sleep(self, retry_num: int) -> None:
|
2020-12-18 07:27:52 -06:00
|
|
|
# Add a random component to avoid synchronized retries
|
|
|
|
wiggle = random.randint(0, 100)
|
|
|
|
sleep_ms = min(
|
2022-03-15 02:19:44 -05:00
|
|
|
self.min_backoff + self.multiplier**retry_num + wiggle, self.max_backoff
|
2020-12-18 07:27:52 -06:00
|
|
|
)
|
|
|
|
time.sleep(sleep_ms / 1000)
|
|
|
|
|
2023-07-12 04:42:17 -05:00
|
|
|
def time_out_message(self) -> str:
|
2020-12-18 07:27:52 -06:00
|
|
|
return (
|
|
|
|
"Tried {} times with increasing delay (from {} to {} milliseconds).".format(
|
|
|
|
self.max_num_retries, self.min_backoff, self.max_backoff
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-07-12 04:42:17 -05:00
|
|
|
def num_retries(self) -> int:
|
2020-12-18 07:27:52 -06:00
|
|
|
return self.max_num_retries
|