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

rectify control of easy-handle callbacks #155

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
75 changes: 26 additions & 49 deletions src/Curl/Easy.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
mutable struct Easy
handle :: Ptr{Cvoid}
input :: Union{Vector{UInt8},Nothing}
ready :: Threads.Event
input :: IO
done :: Threads.Event
seeker :: Union{Function,Nothing}
output :: Channel{Vector{UInt8}}
progress :: Channel{NTuple{4,Int}}
output :: IO
progress :: Function
req_hdrs :: Ptr{curl_slist_t}
res_hdrs :: Vector{String}
code :: CURLcode
errbuf :: Vector{UInt8}
end

const EMPTY_BYTE_VECTOR = UInt8[]

function Easy()
function Easy(
input :: IO,
output :: IO,
progress :: Union{Function,Nothing},
)
easy = Easy(
curl_easy_init(),
EMPTY_BYTE_VECTOR,
input,
Threads.Event(),
nothing,
Channel{Vector{UInt8}}(Inf),
Channel{NTuple{4,Int}}(Inf),
output,
something(progress, (_, _, _, _) -> nothing),
C_NULL,
String[],
typemax(CURLcode),
Expand Down Expand Up @@ -284,50 +286,28 @@ end
# callbacks

function header_callback(
data :: Ptr{Cchar},
data :: Ptr{UInt8},
size :: Csize_t,
count :: Csize_t,
easy_p :: Ptr{Cvoid},
)::Csize_t
easy = unsafe_pointer_to_objref(easy_p)::Easy
n = size * count
n = size*count
hdr = unsafe_string(data, n)
push!(easy.res_hdrs, hdr)
return n
end

# feed data to read_callback
function upload_data(easy::Easy, input::IO)
while true
data = eof(input) ? nothing : readavailable(input)
easy.input === nothing && break
easy.input = data
curl_easy_pause(easy.handle, Curl.CURLPAUSE_CONT)
wait(easy.ready)
easy.input === nothing && break
easy.ready = Threads.Event()
end
end

function read_callback(
data :: Ptr{Cchar},
data :: Ptr{UInt8},
size :: Csize_t,
count :: Csize_t,
easy_p :: Ptr{Cvoid},
)::Csize_t
easy = unsafe_pointer_to_objref(easy_p)::Easy
buf = easy.input
if buf === nothing
notify(easy.ready)
return 0 # done uploading
end
if isempty(buf)
notify(easy.ready)
return CURL_READFUNC_PAUSE # wait for more data
end
n = min(size * count, length(buf))
ccall(:memcpy, Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), data, buf, n)
deleteat!(buf, 1:n)
eof(easy.input) && return 0
Copy link
Member

@vtjnash vtjnash Oct 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can block, so we'll eventually want to add flow-control with something like:

n = UInt(bytesavailable(easy.input))
if n == 0
    errormonitor(@async begin
        eof(easy.input) ? #= notify curl of EOF =# : #= notify curl of more data =#
    end)
    return CURL_READFUNC_PAUSE
end
n = min(n, UInt(size * count))
unsafe_read(easy.input, data, n)
return n

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, most of these implementations can block... that's one of the main drawbacks noted in the PR notes.

buf = unsafe_wrap(Vector{UInt8}, data, size*count)
n = readbytes!(easy.input, buf, size*count)
return n
end

Expand All @@ -344,24 +324,21 @@ function seek_callback(
easy.seeker === nothing && return CURL_SEEKFUNC_CANTSEEK
try easy.seeker(offset)
catch err
@async @error("seek_callback: seeker failed", err)
@async @error("seek_callback: seek failed", err)
return CURL_SEEKFUNC_FAIL
end
return CURL_SEEKFUNC_OK
end

function write_callback(
data :: Ptr{Cchar},
data :: Ptr{UInt8},
size :: Csize_t,
count :: Csize_t,
easy_p :: Ptr{Cvoid},
)::Csize_t
easy = unsafe_pointer_to_objref(easy_p)::Easy
n = size * count
buf = Array{UInt8}(undef, n)
ccall(:memcpy, Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), buf, data, n)
put!(easy.output, buf)
return n
unsafe_write(easy.output, data, size*count)
return size*count
end

function progress_callback(
Expand All @@ -372,7 +349,7 @@ function progress_callback(
ul_now :: curl_off_t,
)::Cint
easy = unsafe_pointer_to_objref(easy_p)::Easy
put!(easy.progress, (dl_total, dl_now, ul_total, ul_now))
easy.progress(dl_total, dl_now, ul_total, ul_now)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid the callback, I'd suggest here doing:

easy.dl_now = dl_now
easy.dl_total = dl_total
easy.ul_total = ul_total
easy.ul_now = ul_now
notify(easy.progress::Event)

return 0
end

Expand All @@ -387,13 +364,13 @@ function add_callbacks(easy::Easy)

# set header callback
header_cb = @cfunction(header_callback,
Csize_t, (Ptr{Cchar}, Csize_t, Csize_t, Ptr{Cvoid}))
Csize_t, (Ptr{UInt8}, Csize_t, Csize_t, Ptr{Cvoid}))
setopt(easy, CURLOPT_HEADERFUNCTION, header_cb)
setopt(easy, CURLOPT_HEADERDATA, easy_p)

# set write callback
write_cb = @cfunction(write_callback,
Csize_t, (Ptr{Cchar}, Csize_t, Csize_t, Ptr{Cvoid}))
Csize_t, (Ptr{UInt8}, Csize_t, Csize_t, Ptr{Cvoid}))
setopt(easy, CURLOPT_WRITEFUNCTION, write_cb)
setopt(easy, CURLOPT_WRITEDATA, easy_p)

Expand All @@ -410,7 +387,7 @@ function add_upload_callbacks(easy::Easy)

# set read callback
read_cb = @cfunction(read_callback,
Csize_t, (Ptr{Cchar}, Csize_t, Csize_t, Ptr{Cvoid}))
Csize_t, (Ptr{UInt8}, Csize_t, Csize_t, Ptr{Cvoid}))
setopt(easy, CURLOPT_READFUNCTION, read_cb)
setopt(easy, CURLOPT_READDATA, easy_p)
end
Expand Down
5 changes: 1 addition & 4 deletions src/Curl/Multi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ function check_multi_info(multi::Multi)
easy = unsafe_pointer_to_objref(easy_p_ref[])::Easy
@assert easy_handle == easy.handle
easy.code = message.code
close(easy.progress)
close(easy.output)
easy.input = nothing
notify(easy.ready)
notify(easy.done)
else
@async @error("curl_multi_info_read: unknown message", message)
end
Expand Down
21 changes: 3 additions & 18 deletions src/Downloads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ function request(
progress = p_func(progress, input, output)
arg_read(input) do input
arg_write(output) do output
with_handle(Easy()) do easy
with_handle(Easy(input, output, progress)) do easy
# setup the request
set_url(easy, url)
set_timeout(easy, timeout)
Expand Down Expand Up @@ -343,23 +343,8 @@ function request(

# do the request
add_handle(downloader.multi, easy)
try # ensure handle is removed
@sync begin
@async for buf in easy.output
write(output, buf)
end
if progress !== nothing
@async for prog in easy.progress
progress(prog...)
end
end
if have_input
@async upload_data(easy, input)
end
end
finally
remove_handle(downloader.multi, easy)
end
wait(easy.done)
remove_handle(downloader.multi, easy)

# return the response or throw an error
response = Response(get_response_info(easy)...)
Expand Down