Skip to content

Commit

Permalink
WIP: Add ReadTimeoutError Exception (#693)
Browse files Browse the repository at this point in the history
* add Readtimeoutexception

* add tests

* Update client.jl
  • Loading branch information
ueliwechsler authored Mar 17, 2021
1 parent c10ae67 commit 2a03ca7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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

0 comments on commit 2a03ca7

Please sign in to comment.