API refactor
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-10-07 16:25:52 +09:00
parent 76d0d86211
commit 91c7e04474
1171 changed files with 81940 additions and 44117 deletions

View File

@@ -1,17 +1,27 @@
import abc
import socket
from time import sleep
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)
class Retry:
if TYPE_CHECKING:
from redis.backoff import AbstractBackoff
class AbstractRetry(Generic[E], abc.ABC):
"""Retry a specific number of times after a failure"""
_supported_errors: Tuple[Type[E], ...]
def __init__(
self,
backoff,
retries,
supported_errors=(ConnectionError, TimeoutError, socket.timeout),
backoff: "AbstractBackoff",
retries: int,
supported_errors: Tuple[Type[E], ...],
):
"""
Initialize a `Retry` object with a `Backoff` object
@@ -24,7 +34,14 @@ class Retry:
self._retries = retries
self._supported_errors = supported_errors
def update_supported_errors(self, specified_errors: list):
@abc.abstractmethod
def __eq__(self, other: Any) -> bool:
return NotImplemented
def __hash__(self) -> int:
return hash((self._backoff, self._retries, frozenset(self._supported_errors)))
def update_supported_errors(self, specified_errors: Iterable[Type[E]]) -> None:
"""
Updates the supported errors with the specified error types
"""
@@ -32,7 +49,49 @@ class Retry:
set(self._supported_errors + tuple(specified_errors))
)
def call_with_retry(self, do, fail):
def get_retries(self) -> int:
"""
Get the number of retries.
"""
return self._retries
def update_retries(self, value: int) -> None:
"""
Set the number of retries.
"""
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)
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],
fail: Callable[[Exception], Any],
) -> T:
"""
Execute an operation that might fail and returns its result, or
raise the exception that was thrown depending on the `Backoff` object.