Skip to content

Commit

Permalink
Add default small delay to retry.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
samoconnor committed Mar 9, 2016
1 parent 2ca8142 commit 91bde90
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ end


"""
retry(f, [condition], n=3)
retry(f, [condition], n=3; max_delay=10)
Returns a lambda that retries function `f` up to `n` times in the
event of an exception. If `condition` is a `Type` then retry only
Expand All @@ -61,8 +61,9 @@ for exceptions of that type. If `condition` is a function
e.g. `retry(http_get, e->e.status == "503")(url)` or `retry(read, UVError)(io)`.
"""
function retry(f::Function, condition::Function=e->true, n::Int=3)
function retry(f::Function, condition::Function=e->true, n::Int=3; max_delay=10)
(args...) -> begin
delay = 0.05
for i = 1:n
try
return f(args...)
Expand All @@ -71,6 +72,8 @@ function retry(f::Function, condition::Function=e->true, n::Int=3)
rethrow(e)
end
end
sleep(delay * (0.8 + (rand() * 0.4)))
delay = min(max_delay, delay * 5)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ Errors
An error occurred when running a module's ``__init__`` function. The actual error thrown is available in the ``.error`` field.

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

.. Docstring generated from Julia source
Expand Down

0 comments on commit 91bde90

Please sign in to comment.