Skip to content

Releases: kaelzhang/python-aioretry

6.3.0

Choose a tag to compare

@kaelzhang kaelzhang released this 20 Feb 13:41

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())

6.2.0

Choose a tag to compare

@kaelzhang kaelzhang released this 29 Oct 02:46

Since 6.2.0, aioretry supports async retry_policy

async def retry_policy(info: RetryInfo) -> RetryPolicyStrategy:
    ...

@retry(retry_policy)
async def connect_to_server():
    ...

6.0.0

Choose a tag to compare

@kaelzhang kaelzhang released this 30 Sep 13:17

Since 6.0.0, RetryInfo::since is a float value which is

  • generated by time.monotonic()
  • better for measuring intervals than datetime
  • and not affected by system clock updates

while RetryInfo::since is a datetime in 5.x

# 5.x
from datetime import datetime

def retry_policy(info: RetryInfo) -> RetryPolicyStrategy:
    print('error occurred', (datetime.now() - info.since).total_seconds(), 'seconds ago')

    ...
# 6.x
import time

def retry_policy(info: RetryInfo) -> RetryPolicyStrategy:
    print('error occurred', time.monotonic() - info.since, 'seconds ago')

    ...

Suggestion

An upgrade is recommended for all dependents, however please pay attention that 6.0.0 introduced a breaking change to 5.x.