We could also use a classmethod, a staticmethod or a direct method as the retry policy
class ClientWithConfigurableRetryPolicy(Client):
MAX_RETRIES = 3
@classmethod
def class_retry_policy(cls, info: RetryInfo) -> RetryPolicyStrategy:
return info.fails > cls.MAX_RETRIES, info.fails * 0.1
@staticmethod
def static_retry_policy(info: RetryInfo) -> RetryPolicyStrategy:
return info.fails > 3, info.fails * 0.1
def __init__(self, max_retries: int = 3):
self._max_retries = max_retries
def _retry_policy(self, info: RetryInfo) -> RetryPolicyStrategy:
return info.fails > self._max_retries, info.fails * 0.1
# Then aioretry will use `self._retry_policy` as the retry policy.
# And by using a str as the parameter `retry_policy`,
# the decorator must be used for instance methods
@retry('_retry_policy')
async def connect_with_retry_policy_name(self):
await self._connect()
# We could also be able to use a method as the retry policy,
# since 6.3.0
@retry(_retry_policy)
async def connect_with_method_retry_policy(self):
await self._connect()
# We could also use a class method, since 6.3.0
@retry(class_retry_policy)
async def connect_with_class_retry_policy(self):
await self._connect()
# A static method is also allowed, since 6.3.0
@retry(static_retry_policy)
async def connect_with_static_retry_policy(self):
await self._connect()
asyncio.run(ClientWithConfigurableRetryPolicy(10).connect_with_retry_policy_name())
asyncio.run(ClientWithConfigurableRetryPolicy(10).connect_with_method_retry_policy())
asyncio.run(ClientWithConfigurableRetryPolicy().connect_with_class_retry_policy())
asyncio.run(ClientWithConfigurableRetryPolicy().connect_with_static_retry_policy())