-
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
base: master
Are you sure you want to change the base?
Changes from all commits
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,6 +1,6 @@ | ||
import socket | ||
from time import sleep | ||
from typing import TYPE_CHECKING, Any, Callable, Iterable, Tuple, Type, TypeVar | ||
from typing import TYPE_CHECKING, Any, Callable, Iterable, Tuple, Type, TypeVar, Union | ||
|
||
from redis.exceptions import ConnectionError, TimeoutError | ||
|
||
|
@@ -10,18 +10,17 @@ | |
from redis.backoff import AbstractBackoff | ||
|
||
|
||
class Retry: | ||
class AbstractRetry: | ||
"""Retry a specific number of times after a failure""" | ||
|
||
__slots__ = "_backoff", "_retries", "_supported_errors" | ||
_supported_errors: Tuple[Type[Exception], ...] | ||
|
||
def __init__( | ||
self, | ||
backoff: "AbstractBackoff", | ||
retries: int, | ||
supported_errors: Tuple[Type[Exception], ...] = ( | ||
ConnectionError, | ||
TimeoutError, | ||
socket.timeout, | ||
), | ||
supported_errors: Union[Tuple[Type[Exception], ...], None] = None, | ||
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 think it would be better here if you extract the list as a constant and assign this list as the default value - it will be more visible what will be assigned to the list in case the user doesn't provide a value. |
||
): | ||
""" | ||
Initialize a `Retry` object with a `Backoff` object | ||
|
@@ -32,10 +31,11 @@ def __init__( | |
""" | ||
self._backoff = backoff | ||
self._retries = retries | ||
self._supported_errors = supported_errors | ||
if supported_errors: | ||
self._supported_errors = supported_errors | ||
|
||
def __eq__(self, other: Any) -> bool: | ||
if not isinstance(other, Retry): | ||
if not isinstance(other, AbstractRetry): | ||
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 think we should have |
||
return NotImplemented | ||
|
||
return ( | ||
|
@@ -69,6 +69,14 @@ def update_retries(self, value: int) -> None: | |
""" | ||
self._retries = value | ||
|
||
|
||
class Retry(AbstractRetry): | ||
_supported_errors: Tuple[Type[Exception], ...] = ( | ||
ConnectionError, | ||
TimeoutError, | ||
socket.timeout, | ||
) | ||
|
||
def call_with_retry( | ||
self, | ||
do: Callable[[], T], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we don't need to use
__slots__
for theretry
object. We are not keeping that many instances of it, and the saved memory is not going to be significant. I see that we've had both patterns in thesync
andasync
versions, and I suggest sticking to the one that has been used by the syncRetry
class.