Skip to content

Commit

Permalink
Added kwargs support to retry. (JuliaLang#21419)
Browse files Browse the repository at this point in the history
* Added kwargs support to `retry`.

* Updated retry example in doc string and added a couple tests.
  • Loading branch information
Rory Finnegan authored and ararslan committed Apr 19, 2017
1 parent 3a47d4e commit e6f8180
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ retry(f, delays=fill(5.0, 3))
retry(f, delays=rand(5:10, 2))
retry(f, delays=Base.ExponentialBackOff(n=3, first_delay=5, max_delay=1000))
retry(http_get, check=(s,e)->e.status == "503")(url)
retry(read, check=(s,e)->isa(e, UVError))(io)
retry(read, check=(s,e)->isa(e, UVError))(io, 128; all=false)
```
"""
function retry(f::Function; delays=ExponentialBackOff(), check=nothing)
(args...) -> begin
(args...; kwargs...) -> begin
state = start(delays)
while true
try
return f(args...)
return f(args...; kwargs...)
catch e
done(delays, state) && rethrow(e)
if check !== nothing
Expand Down
5 changes: 5 additions & 0 deletions test/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ let
@test typeof(ex) == ErrorException
@test ex.msg == "foo"
@test c[1] == 1

# Functions with keyword arguments
foo_kwargs(x; y=5) = x + y
@test retry(foo_kwargs)(3) == 8
@test retry(foo_kwargs)(3; y=4) == 7
end

0 comments on commit e6f8180

Please sign in to comment.