-
Notifications
You must be signed in to change notification settings - Fork 2.6k
add async Retry __eq__
and __hash__
& fix ExponentialWithJitterBackoff __eq__
#3668
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
Changes from 4 commits
783cfd6
8e800d7
49a630c
bcc5d78
606ef96
7e192e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,27 +1,27 @@ | ||||
import abc | ||||
import socket | ||||
from time import sleep | ||||
from typing import TYPE_CHECKING, Any, Callable, Iterable, Tuple, Type, TypeVar | ||||
from typing import TYPE_CHECKING, Any, Callable, Generic, Iterable, Tuple, Type, TypeVar | ||||
|
||||
from redis.exceptions import ConnectionError, TimeoutError | ||||
|
||||
T = TypeVar("T") | ||||
E = TypeVar("E", bound=Exception, covariant=True) | ||||
|
||||
if TYPE_CHECKING: | ||||
from redis.backoff import AbstractBackoff | ||||
|
||||
|
||||
class Retry: | ||||
class AbstractRetry(Generic[E], abc.ABC): | ||||
"""Retry a specific number of times after a failure""" | ||||
|
||||
_supported_errors: Tuple[Type[E], ...] | ||||
|
||||
def __init__( | ||||
self, | ||||
backoff: "AbstractBackoff", | ||||
retries: int, | ||||
supported_errors: Tuple[Type[Exception], ...] = ( | ||||
ConnectionError, | ||||
TimeoutError, | ||||
socket.timeout, | ||||
), | ||||
supported_errors: Tuple[Type[E], ...], | ||||
): | ||||
""" | ||||
Initialize a `Retry` object with a `Backoff` object | ||||
|
@@ -34,22 +34,14 @@ def __init__( | |||
self._retries = retries | ||||
self._supported_errors = supported_errors | ||||
|
||||
@abc.abstractmethod | ||||
def __eq__(self, other: Any) -> bool: | ||||
if not isinstance(other, Retry): | ||||
return NotImplemented | ||||
|
||||
return ( | ||||
self._backoff == other._backoff | ||||
and self._retries == other._retries | ||||
and set(self._supported_errors) == set(other._supported_errors) | ||||
) | ||||
return NotImplemented | ||||
petyaslavova marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
def __hash__(self) -> int: | ||||
return hash((self._backoff, self._retries, frozenset(self._supported_errors))) | ||||
|
||||
def update_supported_errors( | ||||
self, specified_errors: Iterable[Type[Exception]] | ||||
) -> None: | ||||
def update_supported_errors(self, specified_errors: Iterable[Type[E]]) -> None: | ||||
""" | ||||
Updates the supported errors with the specified error types | ||||
""" | ||||
|
@@ -69,6 +61,34 @@ def update_retries(self, value: int) -> None: | |||
""" | ||||
self._retries = value | ||||
|
||||
|
||||
class Retry(AbstractRetry[Exception]): | ||||
__hash__ = AbstractRetry.__hash__ | ||||
|
||||
def __init__( | ||||
self, | ||||
backoff: "AbstractBackoff", | ||||
retries: int, | ||||
supported_errors: Tuple[Type[Exception], ...] = ( | ||||
ConnectionError, | ||||
TimeoutError, | ||||
socket.timeout, | ||||
), | ||||
): | ||||
super().__init__(backoff, retries, supported_errors) | ||||
|
||||
__init__.__doc__ = AbstractRetry.__init__.__doc__ | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Manually copying docstrings can lead to maintenance issues. Consider using proper inheritance or decorators to share documentation, or simply let the docstring inherit naturally from the parent class.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed both of these, but I'm not sure this is actually correct. This is supposed to be the case for |
||||
def __eq__(self, other: Any) -> bool: | ||||
if not isinstance(other, Retry): | ||||
return NotImplemented | ||||
|
||||
return ( | ||||
self._backoff == other._backoff | ||||
and self._retries == other._retries | ||||
and set(self._supported_errors) == set(other._supported_errors) | ||||
) | ||||
|
||||
def call_with_retry( | ||||
self, | ||||
do: Callable[[], T], | ||||
|
Uh oh!
There was an error while loading. Please reload this page.