Skip to content

Commit ab25f08

Browse files
authored
New retry_check should take response body as arg (#976)
* New retry_check should take response body as arg I realized we can't mess with the Response body field directly when using the new `retry_check` functionality because we might clobber a user-provided `response_stream`, which we set as the Response body field. Instead, it should just be an additional arg to the `retry_check` function. * Fix test * update docs; pass resp as nothing if none
1 parent 125187c commit ab25f08

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

src/HTTP.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,10 @@ Retry arguments:
136136
- `retries = 4`, number of times to retry.
137137
- `retry_non_idempotent = false`, retry non-idempotent requests too. e.g. POST.
138138
- `retry_delay = ExponentialBackOff(n = retries)`, provide a custom `ExponentialBackOff` object to control the delay between retries.
139-
- `retry_check = (s, ex, req, resp) -> Bool`, provide a custom function to control whether a retry should be attempted.
140-
The function should accept 4 arguments: the delay state, exception, request, and response, and return `true` if a retry should be attempted.
139+
- `retry_check = (s, ex, req, resp, resp_body) -> Bool`, provide a custom function to control whether a retry should be attempted.
140+
The function should accept 5 arguments: the delay state, exception, request, response (an `HTTP.Response` object *if* a request was
141+
successfully made, otherwise `nothing`), and `resp_body` response body (which may be `nothing` if there is no response yet, otherwise
142+
a `Vector{UInt8}`), and return `true` if a retry should be attempted.
141143
142144
Redirect arguments:
143145
- `redirect = true`, follow 3xx redirect responses; i.e. additional requests will be made to the redirected location

src/clientlayers/RetryRequest.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,8 @@ end
6969

7070
function _retry_check(s, ex, req, check)
7171
resp = req.response
72-
if haskey(req.context, :response_body)
73-
resp.body = req.context[:response_body]
74-
end
75-
return check(s, ex, req, resp)
72+
resp_body = get(req.context, :response_body, nothing)
73+
return check(s, ex, req, resp_body !== nothing ? resp : nothing, resp_body)
7674
end
7775

7876
function no_retry_reason(ex, req)

test/client.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ end
539539
shouldfail[] = false
540540
status[] = 404
541541
seekstart(req_body)
542-
check = (s, ex, req, resp) -> begin
543-
str = String(resp.body)
542+
check = (s, ex, req, resp, resp_body) -> begin
543+
str = String(resp_body)
544544
if str != "404 unexpected error" || resp.status != 404
545545
@error "unexpected response body" str
546546
return false

0 commit comments

Comments
 (0)