Skip to content

Commit

Permalink
always write to buffer on last attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Nov 23, 2021
1 parent 270925c commit 929d1c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/utilities/downloads_backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ function _http_request(backend::DownloadsBackend, request::Request, response_str
# We pass an `input` only when we have content we wish to send.
input = !isempty(request.content) ? IOBuffer(request.content) : nothing

@repeat 4 try
attempts = 4
attempt = 0
@repeat attempts try
attempt += 1
downloader = @something(backend.downloader, get_downloader())
# set the hook so that we don't follow redirects. Only
# need to do this on per-request downloaders, because we
Expand Down Expand Up @@ -104,9 +107,11 @@ function _http_request(backend::DownloadsBackend, request::Request, response_str
if !isnothing(output)
close(buffer)
# Transfer the contents of the `BufferStream` into `response_stream` variable.
# but only if no EOFError error because of a broken connection.
# but only if no EOFError error because of a broken connection OR it's the final attempt.
# i.e. Multiple EOFError retries shouldn't be passed to the `response_stream`
should_write && write(response_stream, buffer)
if should_write || attempt == attempts
write(response_stream, buffer)
end
end
end

Expand Down
11 changes: 8 additions & 3 deletions src/utilities/request.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ end
function _http_request(http_backend::HTTPBackend, request::Request, response_stream::IO)
http_options = merge(http_backend.http_options, request.http_options)

@repeat 4 try
attempts = 4
attempt = 0
@repeat attempts try
attempt += 1
# HTTP options such as `status_exception` need to be used when creating the stack
http_stack = HTTP.stack(;
redirect=false, retry=false, aws_authorization=false, http_options...
Expand Down Expand Up @@ -181,9 +184,11 @@ function _http_request(http_backend::HTTPBackend, request::Request, response_str
close(buffer)

# Transfer the contents of the `BufferStream` into `response_stream` variable
# but only if no EOFError error because of a broken connection.
# but only if no EOFError error because of a broken connection OR it's the final attempt.
# i.e. Multiple EOFError retries shouldn't be passed to the `response_stream`
should_write && write(response_stream, buffer)
if should_write || attempt == attempts
write(response_stream, buffer)
end
end

return @mock Response(r, response_stream)
Expand Down

0 comments on commit 929d1c4

Please sign in to comment.