Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/backoff/_decorator.py: 33%
30 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
1# coding:utf-8
2import asyncio
3import logging
4import operator
5from typing import Any, Callable, Iterable, Optional, Type, Union
7from backoff._common import (
8 _prepare_logger,
9 _config_handlers,
10 _log_backoff,
11 _log_giveup
12)
13from backoff._jitter import full_jitter
14from backoff import _async, _sync
15from backoff._typing import (
16 _CallableT,
17 _Handler,
18 _Jitterer,
19 _MaybeCallable,
20 _MaybeLogger,
21 _MaybeSequence,
22 _Predicate,
23 _WaitGenerator,
24)
27def on_predicate(wait_gen: _WaitGenerator,
28 predicate: _Predicate[Any] = operator.not_,
29 *,
30 max_tries: Optional[_MaybeCallable[int]] = None,
31 max_time: Optional[_MaybeCallable[float]] = None,
32 jitter: Union[_Jitterer, None] = full_jitter,
33 on_success: Union[_Handler, Iterable[_Handler], None] = None,
34 on_backoff: Union[_Handler, Iterable[_Handler], None] = None,
35 on_giveup: Union[_Handler, Iterable[_Handler], None] = None,
36 logger: _MaybeLogger = 'backoff',
37 backoff_log_level: int = logging.INFO,
38 giveup_log_level: int = logging.ERROR,
39 **wait_gen_kwargs: Any) -> Callable[[_CallableT], _CallableT]:
40 """Returns decorator for backoff and retry triggered by predicate.
42 Args:
43 wait_gen: A generator yielding successive wait times in
44 seconds.
45 predicate: A function which when called on the return value of
46 the target function will trigger backoff when considered
47 truthily. If not specified, the default behavior is to
48 backoff on falsey return values.
49 max_tries: The maximum number of attempts to make before giving
50 up. In the case of failure, the result of the last attempt
51 will be returned. The default value of None means there
52 is no limit to the number of tries. If a callable is passed,
53 it will be evaluated at runtime and its return value used.
54 max_time: The maximum total amount of time to try for before
55 giving up. If this time expires, the result of the last
56 attempt will be returned. If a callable is passed, it will
57 be evaluated at runtime and its return value used.
58 jitter: A function of the value yielded by wait_gen returning
59 the actual time to wait. This distributes wait times
60 stochastically in order to avoid timing collisions across
61 concurrent clients. Wait times are jittered by default
62 using the full_jitter function. Jittering may be disabled
63 altogether by passing jitter=None.
64 on_success: Callable (or iterable of callables) with a unary
65 signature to be called in the event of success. The
66 parameter is a dict containing details about the invocation.
67 on_backoff: Callable (or iterable of callables) with a unary
68 signature to be called in the event of a backoff. The
69 parameter is a dict containing details about the invocation.
70 on_giveup: Callable (or iterable of callables) with a unary
71 signature to be called in the event that max_tries
72 is exceeded. The parameter is a dict containing details
73 about the invocation.
74 logger: Name of logger or Logger object to log to. Defaults to
75 'backoff'.
76 backoff_log_level: log level for the backoff event. Defaults to "INFO"
77 giveup_log_level: log level for the give up event. Defaults to "ERROR"
78 **wait_gen_kwargs: Any additional keyword args specified will be
79 passed to wait_gen when it is initialized. Any callable
80 args will first be evaluated and their return values passed.
81 This is useful for runtime configuration.
82 """
83 def decorate(target):
84 nonlocal logger, on_success, on_backoff, on_giveup
86 logger = _prepare_logger(logger)
87 on_success = _config_handlers(on_success)
88 on_backoff = _config_handlers(
89 on_backoff,
90 default_handler=_log_backoff,
91 logger=logger,
92 log_level=backoff_log_level
93 )
94 on_giveup = _config_handlers(
95 on_giveup,
96 default_handler=_log_giveup,
97 logger=logger,
98 log_level=giveup_log_level
99 )
101 if asyncio.iscoroutinefunction(target):
102 retry = _async.retry_predicate
103 else:
104 retry = _sync.retry_predicate
106 return retry(
107 target,
108 wait_gen,
109 predicate,
110 max_tries=max_tries,
111 max_time=max_time,
112 jitter=jitter,
113 on_success=on_success,
114 on_backoff=on_backoff,
115 on_giveup=on_giveup,
116 wait_gen_kwargs=wait_gen_kwargs
117 )
119 # Return a function which decorates a target with a retry loop.
120 return decorate
123def on_exception(wait_gen: _WaitGenerator,
124 exception: _MaybeSequence[Type[Exception]],
125 *,
126 max_tries: Optional[_MaybeCallable[int]] = None,
127 max_time: Optional[_MaybeCallable[float]] = None,
128 jitter: Union[_Jitterer, None] = full_jitter,
129 giveup: _Predicate[Exception] = lambda e: False,
130 on_success: Union[_Handler, Iterable[_Handler], None] = None,
131 on_backoff: Union[_Handler, Iterable[_Handler], None] = None,
132 on_giveup: Union[_Handler, Iterable[_Handler], None] = None,
133 raise_on_giveup: bool = True,
134 logger: _MaybeLogger = 'backoff',
135 backoff_log_level: int = logging.INFO,
136 giveup_log_level: int = logging.ERROR,
137 **wait_gen_kwargs: Any) -> Callable[[_CallableT], _CallableT]:
138 """Returns decorator for backoff and retry triggered by exception.
140 Args:
141 wait_gen: A generator yielding successive wait times in
142 seconds.
143 exception: An exception type (or tuple of types) which triggers
144 backoff.
145 max_tries: The maximum number of attempts to make before giving
146 up. Once exhausted, the exception will be allowed to escape.
147 The default value of None means there is no limit to the
148 number of tries. If a callable is passed, it will be
149 evaluated at runtime and its return value used.
150 max_time: The maximum total amount of time to try for before
151 giving up. Once expired, the exception will be allowed to
152 escape. If a callable is passed, it will be
153 evaluated at runtime and its return value used.
154 jitter: A function of the value yielded by wait_gen returning
155 the actual time to wait. This distributes wait times
156 stochastically in order to avoid timing collisions across
157 concurrent clients. Wait times are jittered by default
158 using the full_jitter function. Jittering may be disabled
159 altogether by passing jitter=None.
160 giveup: Function accepting an exception instance and
161 returning whether or not to give up. Optional. The default
162 is to always continue.
163 on_success: Callable (or iterable of callables) with a unary
164 signature to be called in the event of success. The
165 parameter is a dict containing details about the invocation.
166 on_backoff: Callable (or iterable of callables) with a unary
167 signature to be called in the event of a backoff. The
168 parameter is a dict containing details about the invocation.
169 on_giveup: Callable (or iterable of callables) with a unary
170 signature to be called in the event that max_tries
171 is exceeded. The parameter is a dict containing details
172 about the invocation.
173 raise_on_giveup: Boolean indicating whether the registered exceptions
174 should be raised on giveup. Defaults to `True`
175 logger: Name or Logger object to log to. Defaults to 'backoff'.
176 backoff_log_level: log level for the backoff event. Defaults to "INFO"
177 giveup_log_level: log level for the give up event. Defaults to "ERROR"
178 **wait_gen_kwargs: Any additional keyword args specified will be
179 passed to wait_gen when it is initialized. Any callable
180 args will first be evaluated and their return values passed.
181 This is useful for runtime configuration.
182 """
183 def decorate(target):
184 nonlocal logger, on_success, on_backoff, on_giveup
186 logger = _prepare_logger(logger)
187 on_success = _config_handlers(on_success)
188 on_backoff = _config_handlers(
189 on_backoff,
190 default_handler=_log_backoff,
191 logger=logger,
192 log_level=backoff_log_level,
193 )
194 on_giveup = _config_handlers(
195 on_giveup,
196 default_handler=_log_giveup,
197 logger=logger,
198 log_level=giveup_log_level,
199 )
201 if asyncio.iscoroutinefunction(target):
202 retry = _async.retry_exception
203 else:
204 retry = _sync.retry_exception
206 return retry(
207 target,
208 wait_gen,
209 exception,
210 max_tries=max_tries,
211 max_time=max_time,
212 jitter=jitter,
213 giveup=giveup,
214 on_success=on_success,
215 on_backoff=on_backoff,
216 on_giveup=on_giveup,
217 raise_on_giveup=raise_on_giveup,
218 wait_gen_kwargs=wait_gen_kwargs
219 )
221 # Return a function which decorates a target with a retry loop.
222 return decorate