@@ -139,15 +139,48 @@ def retry(initial_delay,
139
139
140
140
Args:
141
141
initial_delay: the initial delay.
142
+ max_delay: the maximum delay allowed (actual max is
143
+ max_delay * (1 + jitter).
142
144
factor: each subsequent retry, the delay is multiplied by this value.
143
145
(must be >= 1).
144
146
jitter: to avoid lockstep, the returned delay is multiplied by a random
145
147
number between (1-jitter) and (1+jitter). To add a 20% jitter, set
146
148
jitter = 0.2. Must be < 1.
149
+ is_retriable: (optional) a function that takes an Exception as an argument
150
+ and returns true if retry should be applied.
151
+
152
+ Returns:
153
+ A function that wraps another function to automatically retry it.
154
+ """
155
+ return _internal_retry (
156
+ initial_delay = initial_delay ,
157
+ max_delay = max_delay ,
158
+ factor = factor ,
159
+ jitter = jitter ,
160
+ is_retriable = is_retriable )
161
+
162
+
163
+ def _internal_retry (initial_delay ,
164
+ max_delay ,
165
+ factor = 2.0 ,
166
+ jitter = 0.25 ,
167
+ is_retriable = None ):
168
+ """Simple decorator for wrapping retriable functions, for internal use only.
169
+
170
+ Args:
171
+ initial_delay: the initial delay.
147
172
max_delay: the maximum delay allowed (actual max is
148
173
max_delay * (1 + jitter).
174
+ factor: each subsequent retry, the delay is multiplied by this value.
175
+ (must be >= 1).
176
+ jitter: to avoid lockstep, the returned delay is multiplied by a random
177
+ number between (1-jitter) and (1+jitter). To add a 20% jitter, set
178
+ jitter = 0.2. Must be < 1.
149
179
is_retriable: (optional) a function that takes an Exception as an argument
150
180
and returns true if retry should be applied.
181
+
182
+ Returns:
183
+ A function that wraps another function to automatically retry it.
151
184
"""
152
185
if factor < 1 :
153
186
raise ValueError ('factor must be >= 1; was %f' % (factor ,))
@@ -195,7 +228,7 @@ def _is_retriable(e):
195
228
196
229
197
230
@deprecated (None , 'Please use urllib or similar directly.' )
198
- @retry (initial_delay = 1.0 , max_delay = 16.0 , is_retriable = _is_retriable )
231
+ @_internal_retry (initial_delay = 1.0 , max_delay = 16.0 , is_retriable = _is_retriable )
199
232
def urlretrieve_with_retry (url , filename = None ):
200
233
return urllib .request .urlretrieve (url , filename )
201
234
0 commit comments