Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent error message in case of giveup using #217

Open
lesnik512 opened this issue Mar 29, 2024 · 0 comments
Open

Prevent error message in case of giveup using #217

lesnik512 opened this issue Mar 29, 2024 · 0 comments

Comments

@lesnik512
Copy link

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 #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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant