1
1
import socket
2
2
from time import sleep
3
+ from typing import TYPE_CHECKING , Any , Callable , Iterable , Tuple , Type , TypeVar
3
4
4
5
from redis .exceptions import ConnectionError , TimeoutError
5
6
7
+ T = TypeVar ("T" )
8
+
9
+ if TYPE_CHECKING :
10
+ from redis .backoff import AbstractBackoff
11
+
6
12
7
13
class Retry :
8
14
"""Retry a specific number of times after a failure"""
9
15
10
16
def __init__ (
11
17
self ,
12
- backoff ,
13
- retries ,
14
- supported_errors = (ConnectionError , TimeoutError , socket .timeout ),
18
+ backoff : "AbstractBackoff" ,
19
+ retries : int ,
20
+ supported_errors : Tuple [Type [Exception ], ...] = (
21
+ ConnectionError ,
22
+ TimeoutError ,
23
+ socket .timeout ,
24
+ ),
15
25
):
16
26
"""
17
27
Initialize a `Retry` object with a `Backoff` object
@@ -24,15 +34,21 @@ def __init__(
24
34
self ._retries = retries
25
35
self ._supported_errors = supported_errors
26
36
27
- def update_supported_errors (self , specified_errors : list ):
37
+ def update_supported_errors (
38
+ self , specified_errors : Iterable [Type [Exception ]]
39
+ ) -> None :
28
40
"""
29
41
Updates the supported errors with the specified error types
30
42
"""
31
43
self ._supported_errors = tuple (
32
44
set (self ._supported_errors + tuple (specified_errors ))
33
45
)
34
46
35
- def call_with_retry (self , do , fail ):
47
+ def call_with_retry (
48
+ self ,
49
+ do : Callable [[], T ],
50
+ fail : Callable [[Exception ], Any ],
51
+ ) -> T :
36
52
"""
37
53
Execute an operation that might fail and returns its result, or
38
54
raise the exception that was thrown depending on the `Backoff` object.
0 commit comments