backoff icon indicating copy to clipboard operation
backoff copied to clipboard

Prevent error message in case of giveup using

Open lesnik512 opened this issue 2 years ago • 0 comments

Hello, thank you for a grate tool!

Is it possible to not write error messages in case of giving up due to using giveup attribute?

Case is similar to this https://github.com/litl/backoff/issues/108, when I need to check for some fields of exceptions to decide:

def _connection_retry_backoff_handler(exception: Exception) -> bool:
    if (
        isinstance(exception, DBAPIError)
        and hasattr(exception, "orig")
        and isinstance(exception.orig.__cause__, asyncpg.PostgresConnectionError)  # type: ignore[union-attr]
    ):
        logger.debug("postgres_reconnect, backoff triggered")
        return False

    logger.debug("postgres_reconnect, giving up on backoff")
    return True


def postgres_reconnect(func: typing.Callable[P, typing.Awaitable[T]]) -> typing.Callable[P, typing.Awaitable[T]]:
    @backoff.on_exception(
        backoff.expo,
        (DBAPIError,),
        max_tries=settings.DB_UTILS_CONNECTION_TRIES,
        giveup=_connection_retry_backoff_handler,
    )
    @functools.wraps(func)
    async def wrapped_method(*args: P.args, **kwargs: P.kwargs) -> T:
        return await func(*args, **kwargs)

    return wrapped_method

Because now I receive this

Giving up create_user(...) after 1 tries

lesnik512 avatar Mar 29 '24 12:03 lesnik512