Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for issue #988 --> seemles highly customizable retry setup #989

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions src/clientlayers/RetryRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ module RetryRequest
using Sockets, LoggingExtras, MbedTLS, OpenSSL
using ..IOExtras, ..Messages, ..Strings, ..ExceptionRequest, ..Exceptions

export retrylayer

FALSE(x...) = false
export retrylayer, retry_check, isrecoverable, isstatuserror

"""
retrylayer(handler) -> handler
Expand All @@ -21,13 +19,12 @@ e.g. `Sockets.DNSError`, `Base.EOFError` and `HTTP.StatusError`
(if status is `5xx`).
"""
function retrylayer(handler)
return function(req::Request; retry::Bool=true, retries::Int=4,
retry_delays::ExponentialBackOff=ExponentialBackOff(n = retries, factor=3.0), retry_check=FALSE,
retry_non_idempotent::Bool=false, kw...)
if !retry || retries == 0
# no retry
return handler(req; kw...)
end
return function(req::Request; retries::Int=4, retry::Bool=retries>0,
retry_delays=ExponentialBackOff(n=retries, factor=3.0), retry_check=retry_check,
retry_non_idempotent::Bool=false, kw...
)
retry || return handler(req; kw...) # no retry
retries = length(retry_delays)
req.context[:allow_retries] = true
req.context[:retryattempt] = 0
if retry_non_idempotent
Expand All @@ -39,16 +36,10 @@ function retrylayer(handler)
req_body_is_marked = true
mark(req.body)
end
retryattempt = Ref(0)
retry_request = Base.retry(handler,
delays=retry_delays,
check=(s, ex) -> begin
retryattempt[] += 1
req.context[:retryattempt] = retryattempt[]
retry = retryable(req) || retryablebody(req) && _retry_check(s, ex, req, retry_check)
if retryattempt[] == retries
req.context[:retrylimitreached] = true
end
retry_request = Base.retry(handler, delays=retry_delays,
check = (s, ex) -> begin
req.context[:retrylimitreached] = (req.context[:retryattempt] += 1) > retries
retry = retryable(req) && _retry_check(s, ex, req, retry_check)
if retry
@debugv 1 "🔄 Retry $ex: $(sprintcompact(req))"
reset!(req.response)
Expand All @@ -67,6 +58,11 @@ function retrylayer(handler)
end
end

isstatuserror(e, status) = e isa HTTP.StatusError && e.status in status
isrecoverable(ex) = true
isrecoverable(ex::StatusError) = retryable(ex.status)
retry_check(s, ex, x...) = isrecoverable(ex)

function _retry_check(s, ex, req, check)
resp = req.response
resp_body = get(req.context, :response_body, nothing)
Expand Down