Skip to content

Commit

Permalink
allow long polling request to be interrupted
Browse files Browse the repository at this point in the history
Allow long polling request to be interrupted by closing the output channel.
There is no ready API/mechanism in Downloads.jl to interrupt a request.
Here we just throw an InterruptedException to the task that invoked Downloads.request.

Note: A better mechanism may be to invoke `remove_handle(multi::Multi, easy::Easy)`, but that should probably be through an interruption mechanism provided by Downloads.jl?
  • Loading branch information
tanmaykm committed Oct 25, 2021
1 parent 35da247 commit f318005
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,20 @@ function do_request(ctx::Ctx, stream::Bool=false; stream_to::Union{Channel,Nothi
try
if stream
@sync begin
@async begin
download_task = @async begin
try
resp = Downloads.request(resource_path;
input=input,
output=output,
kwargs...
)
close(output)
catch ex
@error("exception invoking request", exception=(ex,catch_backtrace()))
rethrow()
if !isa(ex, InterruptedException)
@error("exception invoking request", exception=(ex,catch_backtrace()))
rethrow()
end
finally
close(output)
end
end
@async begin
Expand All @@ -304,10 +307,24 @@ function do_request(ctx::Ctx, stream::Bool=false; stream_to::Union{Channel,Nothi
data = response(return_type, resp, chunk)
put!(stream_to, data)
end
close(stream_to)
catch ex
@error("exception reading chunk", exception=(ex,catch_backtrace()))
rethrow()
if !isa(ex, InvalidStateException)
@error("exception reading chunk", exception=(ex,catch_backtrace()))
rethrow()
end
finally
close(stream_to)
end
end
@async begin
while isopen(stream_to)
try
wait(stream_to)
yield()
catch ex
isa(ex, InvalidStateException) || rethrow(ex)
istaskdone(download_task) || schedule(download_task, InterruptException(), error=true)
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions test/petstore/test_StoreApi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ function test(uri)
end
end

# a closed channel is equivalent of cancellation of the call, no error should be thrown
@test !isopen(response_channel)
resp = getOrderById(api, response_channel, 10)
@test (200 <= resp.status <= 206)

@info("StoreApi - deleteOrder")
@test deleteOrder(api, 10) === nothing

Expand Down

0 comments on commit f318005

Please sign in to comment.