Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/redis/asyncio/retry.py: 32%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

47 statements  

1import warnings 

2from asyncio import sleep 

3from inspect import iscoroutinefunction 

4from typing import ( 

5 TYPE_CHECKING, 

6 Any, 

7 Awaitable, 

8 Callable, 

9 Tuple, 

10 Type, 

11 TypeVar, 

12 Union, 

13) 

14 

15from redis.exceptions import ConnectionError, TimeoutError 

16from redis.retry import AbstractRetry 

17 

18T = TypeVar("T") 

19 

20if TYPE_CHECKING: 

21 from redis.backoff import AbstractBackoff 

22 

23 

24class Retry(AbstractRetry[Exception]): 

25 __hash__ = AbstractRetry.__hash__ 

26 

27 def __init__( 

28 self, 

29 backoff: "AbstractBackoff", 

30 retries: int, 

31 supported_errors: Tuple[Type[Exception], ...] = ( 

32 ConnectionError, 

33 TimeoutError, 

34 ), 

35 is_retryable: Callable[[Exception], bool] | None = None, 

36 ): 

37 super().__init__(backoff, retries, supported_errors, is_retryable) 

38 

39 def __eq__(self, other: Any) -> bool: 

40 if not isinstance(other, Retry): 

41 return NotImplemented 

42 

43 return ( 

44 self._backoff == other._backoff 

45 and self._retries == other._retries 

46 and set(self._supported_errors) == set(other._supported_errors) 

47 and self._is_retryable == other._is_retryable 

48 ) 

49 

50 async def call_with_retry( 

51 self, 

52 do: Callable[[], Awaitable[T]], 

53 fail: Union[ 

54 Callable[[Exception], Any], 

55 Callable[[Exception, int], Any], 

56 ], 

57 is_retryable: Callable[[Exception], bool] | None = None, 

58 with_failure_count: bool = False, 

59 ) -> T: 

60 """ 

61 Execute an operation that might fail and returns its result, or 

62 raise the exception that was thrown depending on the `Backoff` object. 

63 `do`: the operation to call. Expects no argument. 

64 `fail`: the failure handler, expects the last error that was thrown 

65 ``is_retryable``: optional function to determine if an error is retryable. 

66 It is composed with the checker set on this object (if any) using AND: 

67 an error is retried only if both checkers return True. 

68 ``with_failure_count``: if True, the failure count is passed to the failure handler 

69 """ 

70 self._backoff.reset() 

71 failures = 0 

72 while True: 

73 try: 

74 return await do() 

75 except self._supported_errors as error: 

76 if self._is_retryable is not None and not self._is_retryable(error): 

77 # Run the failure handler before raising so connection-scoped 

78 # cleanup (for example a pipeline's WATCH state) still happens. 

79 if with_failure_count: 

80 await fail(error, failures + 1) 

81 else: 

82 await fail(error) 

83 raise 

84 if is_retryable is not None and not is_retryable(error): 

85 raise 

86 failures += 1 

87 

88 if with_failure_count: 

89 await fail(error, failures) 

90 else: 

91 await fail(error) 

92 

93 if self._retries >= 0 and failures > self._retries: 

94 raise error 

95 backoff = self._backoff.compute(failures) 

96 if backoff > 0: 

97 await sleep(backoff) 

98 

99 

100def _to_async_retry(retry: Any) -> Any: 

101 """Convert a synchronous retry policy while preserving async-shaped ones.""" 

102 if iscoroutinefunction(getattr(type(retry), "call_with_retry", None)): 

103 return retry 

104 if not isinstance(retry, AbstractRetry): 

105 return retry 

106 

107 warnings.warn( 

108 "A synchronous redis.retry.Retry was passed to an asyncio client and has " 

109 "been converted to redis.asyncio.retry.Retry. A custom call_with_retry " 

110 "implementation is not preserved - please use redis.asyncio.retry.Retry.", 

111 UserWarning, 

112 stacklevel=2, 

113 ) 

114 

115 return Retry( 

116 backoff=retry._backoff, 

117 retries=retry._retries, 

118 supported_errors=retry._supported_errors, 

119 is_retryable=getattr(retry, "_is_retryable", None), 

120 )