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

WIP: Add ReadTimeoutError Exception #693

Merged
merged 4 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions src/TimeoutRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import ..Layer, ..request
using ..ConnectionPool
import ..@debug, ..DEBUG_LEVEL

struct ReadTimeoutError <:Exception
readtimeout::Int
end

function Base.showerror(io::IO, e::ReadTimeoutError)
print(io, "ReadTimeoutError: Connection closed after $(e.readtimeout) seconds")
end

"""
request(TimeoutLayer, ::IO, ::Request, body) -> HTTP.Response

Expand All @@ -16,9 +24,11 @@ function request(::Type{TimeoutLayer{Next}}, io::IO, req, body;
readtimeout::Int=0, kw...) where Next

wait_for_timeout = Ref{Bool}(true)
timedout = Ref{Bool}(false)

@async while wait_for_timeout[]
if isreadable(io) && inactiveseconds(io) > readtimeout
timedout[] = true
close(io)
@debug 1 "💥 Read inactive > $(readtimeout)s: $io"
break
Expand All @@ -28,6 +38,11 @@ function request(::Type{TimeoutLayer{Next}}, io::IO, req, body;

try
return request(Next, io, req, body; kw...)
catch e
if timedout[]
throw(ReadTimeoutError(readtimeout))
end
rethrow(e)
finally
wait_for_timeout[] = false
end
Expand Down
5 changes: 4 additions & 1 deletion test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ end
end

@testset "readtimeout" begin
@test_throws HTTP.IOError HTTP.get("http://httpbin.org/delay/5"; readtimeout=1, retry=false)
@test_throws HTTP.TimeoutRequest.ReadTimeoutError begin
HTTP.get("http://httpbin.org/delay/5"; readtimeout=1, retry=false)
end
HTTP.get("http://httpbin.org/delay/1"; readtimeout=2, retry=false)
end

@testset "Retry all resolved IP addresses" begin
Expand Down