Skip to content

Commit acbe45e

Browse files
committed
Add default small delay to retry.
50ms delay on first retry. 250ms delay on 2nd retry. This at least gives other tasks a chance to run. If retry n is set higher, the delay increases to 1250ms, 6250ms ... max_delay caps the dealy at 10s by default. This should handle network-timescale issues without creating undue load.
1 parent 41a317d commit acbe45e

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

base/error.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ end
5252

5353

5454
"""
55-
retry(f, [condition], n=3)
55+
retry(f, [condition], n=3; max_delay=10)
5656
5757
Returns a lambda that retries function `f` up to `n` times in the
5858
event of an exception. If `condition` is a `Type` then retry only
@@ -61,8 +61,9 @@ for exceptions of that type. If `condition` is a function
6161
6262
e.g. `retry(http_get, e->e.status == "503")(url)` or `retry(read, UVError)(io)`.
6363
"""
64-
function retry(f::Function, condition::Function=e->true, n::Int=3)
64+
function retry(f::Function, condition::Function=e->true, n::Int=3; max_delay=10)
6565
(args...) -> begin
66+
delay = 0.05
6667
for i = 1:n
6768
try
6869
return f(args...)
@@ -71,6 +72,8 @@ function retry(f::Function, condition::Function=e->true, n::Int=3)
7172
rethrow(e)
7273
end
7374
end
75+
sleep(delay * (0.8 + (rand() * 0.4)))
76+
delay = min(max_delay, delay * 5)
7477
end
7578
end
7679
end

doc/stdlib/base.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ Errors
12181218
12191219
An error occurred when running a module's ``__init__`` function. The actual error thrown is available in the ``.error`` field.
12201220

1221-
.. function:: retry(f, [condition], n=3)
1221+
.. function:: retry(f, [condition], n=3; max_delay=10)
12221222

12231223
.. Docstring generated from Julia source
12241224

0 commit comments

Comments
 (0)