JuliaWeb / HTTP.jl

HTTP for Julia
https://juliaweb.github.io/HTTP.jl/stable/
Other
626 stars 177 forks source link

Asynchronous http.get is very slow. #1095

Open lucasmsoares96 opened 10 months ago

lucasmsoares96 commented 10 months ago

In version v1.3.1 of the HTTP package, the code below ran in approximately 70 seconds. After updating the package to version 1.9.14, the execution time became unacceptable. I've waited for 5 minutes and the execution hasn't finished.

Environment:

using CSV, HTTP, Base.Threads, HTTP, Tables

const conf = (readtimeout=10, retry=false)

function testHTTP(proxy::String; testURL::String)::Bool
    try
        resp::HTTP.Messages.Response = HTTP.get(testURL; proxy="http://$proxy", conf...)
        if resp.status == 200
            println(threadid(), "\tok\t", proxy)
            return true
        else
            println(threadid(), "\tbad\t", proxy)
            return false
        end
    catch e
        println(threadid(), "\tfail\t", proxy)
        # println(e)
        return false
    end
end

function asyncfilter(f::Function, i::Vector{String})::Vector{String}
    if length(i) >= 2
        head = i[1]
        tail = i[2:end]
        fun = @async f(head)
        recursion = @async asyncfilter(f, tail)
        if fetch(fun)
            return [head, fetch(recursion)...]
        else
            return [fetch(recursion)...]
        end
    else
        return i
    end
end

function proxyTest(proxies::Vector{String}; testURL::String)::Vector{String}
    chunkSize::Int64 = cld(length(proxies), nthreads())
    chunks::Vector{Vector{String}} = collect(Iterators.partition(proxies, chunkSize))
    result::Vector{String} = []
    tasks::Vector{Task} = []
    sizehint!(tasks, nthreads())
    for chunk in chunks
        push!(
            tasks,
            @spawn asyncfilter(i -> testHTTP(i; testURL=testURL), chunk)
        )
    end
    for task in tasks
        append!(result, fetch(task))
    end
    return result
end

url = "https://paste.debian.net/plain/1251148"
response = HTTP.get(url)

# Convertendo para vetor
allproxies::Vector{String} = split(String(response.body), '\n')[2:end-1]

@time proxyTest(allproxies; testURL="https://www.google.com");