-
Notifications
You must be signed in to change notification settings - Fork 9
/
async_retrying.py
205 lines (149 loc) · 5.49 KB
/
async_retrying.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import asyncio
import copy
import inspect
import logging
from functools import wraps
import async_timeout
logger = logging.getLogger(__name__)
__version__ = '0.2.2'
propagate = forever = ...
class RetryError(Exception):
pass
class ConditionError(Exception):
pass
def unpartial(fn):
while hasattr(fn, 'func'):
fn = fn.func
return fn
def isexception(obj):
return (
isinstance(obj, Exception) or
(inspect.isclass(obj) and (issubclass(obj, Exception)))
)
@asyncio.coroutine
def callback(attempt, exc, args, kwargs, delay=None, *, loop):
if delay is None:
delay = callback.delay
yield from asyncio.sleep(attempt * delay, loop=loop)
return retry
callback.delay = 0.5
def retry(
fn=None,
*,
attempts=3,
immutable=False,
cls=False,
kwargs=False,
callback=callback,
fallback=RetryError,
timeout=None,
retry_exceptions=(Exception,),
fatal_exceptions=(asyncio.CancelledError,),
loop=None # noqa
):
def wrapper(fn):
@wraps(fn)
@asyncio.coroutine
def wrapped(*fn_args, **fn_kwargs):
if isinstance(loop, str):
assert cls ^ kwargs, 'choose self.loop or kwargs["loop"]'
if cls:
_self = getattr(unpartial(fn), '__self__', None)
if _self is None:
assert fn_args, 'seems not unbound function'
_self = fn_args[0]
_loop = getattr(_self, loop)
elif kwargs:
_loop = fn_kwargs[loop]
elif loop is None:
_loop = asyncio.get_event_loop()
else:
_loop = loop
if (
timeout is not None and
asyncio.TimeoutError not in retry_exceptions
):
_retry_exceptions = (asyncio.TimeoutError,) + retry_exceptions
else:
_retry_exceptions = retry_exceptions
attempt = 1
if cls:
assert fn_args
self, *fn_args = fn_args
fn_args = tuple(fn_args)
while True:
if immutable:
_fn_args = copy.deepcopy(fn_args)
kwargs_loop = isinstance(loop, str) and kwargs
if kwargs_loop:
obj = fn_kwargs.pop(loop)
_fn_kwargs = copy.deepcopy(fn_kwargs)
if kwargs_loop:
fn_kwargs[loop] = _fn_kwargs[loop] = obj
else:
_fn_args, _fn_kwargs = fn_args, fn_kwargs
if cls:
_fn_args = (self,) + _fn_args
try:
ret = fn(*_fn_args, **_fn_kwargs)
if timeout is None:
if asyncio.iscoroutinefunction(unpartial(fn)):
ret = yield from ret
else:
if not asyncio.iscoroutinefunction(unpartial(fn)):
raise ConditionError(
'Can\'t set timeout for non coroutinefunction',
)
with async_timeout.timeout(timeout, loop=_loop):
ret = yield from ret
return ret
except ConditionError:
raise
except fatal_exceptions:
raise
except _retry_exceptions as exc:
_attempts = 'infinity' if attempts is forever else attempts
context = {
'fn': fn,
'attempt': attempt,
'attempts': _attempts,
}
if (
_loop.get_debug() or
(attempts is not forever and attempt == attempts)
):
logger.warning(
exc.__class__.__name__ + ' -> Attempts (%(attempt)d) are over for %(fn)r', # noqa
context,
exc_info=exc,
)
if fallback is propagate:
raise exc
if isexception(fallback):
raise fallback from exc
if callable(fallback):
ret = fallback(fn_args, fn_kwargs, loop=_loop)
if asyncio.iscoroutinefunction(unpartial(fallback)): # noqa
ret = yield from ret
else:
ret = fallback
return ret
logger.debug(
exc.__class__.__name__ + ' -> Tried attempt #%(attempt)d from total %(attempts)s for %(fn)r', # noqa
context,
exc_info=exc,
)
ret = callback(
attempt, exc, fn_args, fn_kwargs, loop=_loop,
)
attempt += 1
if asyncio.iscoroutinefunction(unpartial(callback)):
ret = yield from ret
if ret is not retry:
return ret
return wrapped
if fn is None:
return wrapper
if callable(fn):
return wrapper(fn)
raise NotImplementedError