Skip to content

Using interactive thread pool for latency-sensitive route #1010

Closed
@simsurace

Description

@simsurace

This is my (failed) attempt at separating light vs. heavy workloads in different threads to retain responsiveness:

using Dates, HTTP

const ROUTER = HTTP.Router()

function low_latency(req)
    Threads.@spawn :interactive begin # also tried without this
        @info "$(now())> $(Threads.threadpool()) tid: $(Threads.threadid())"
        return "Done with low latency task"
    end
end
HTTP.register!(ROUTER, "GET", "/low", low_latency)

function high_latency(req)
    Threads.@spawn begin
        s = sum(randn(10^8))
        @info "$(now())> $(Threads.threadpool()) tid: $(Threads.threadid()), sum = $s"
        return "Done with high latency task"
    end
end
HTTP.register!(ROUTER, "GET", "/high", high_latency)

function requestHandler(req)
    resp = ROUTER(req)
    return HTTP.Response(200, resp)
end

HTTP.serve(requestHandler, "0.0.0.0", 7777)
┌ Error: handle_connection handler error
│   exception =
│    AssertionError: 0 < tid <= v
│    Stacktrace:
│      [1] _length_assert()
│        @ URIs ~/.julia/packages/URIs/6ecRe/src/URIs.jl:694
│      [2] access_threaded(f::typeof(URIs.uri_reference_regex_f), v::Vector{URIs.RegexAndMatchData})
│        @ URIs ~/.julia/packages/URIs/6ecRe/src/URIs.jl:685
│      [3] parse_uri_reference(str::String; strict::Bool)
│        @ URIs ~/.julia/packages/URIs/6ecRe/src/URIs.jl:125
│      [4] parse_uri_reference
│        @ ~/.julia/packages/URIs/6ecRe/src/URIs.jl:123 [inlined]
│      [5] URI
│        @ ~/.julia/packages/URIs/6ecRe/src/URIs.jl:146 [inlined]
│      [6] gethandler
│        @ ~/.julia/packages/HTTP/z8l0i/src/Handlers.jl:391 [inlined]
│      [7] (::HTTP.Handlers.Router{typeof(HTTP.Handlers.default404), typeof(HTTP.Handlers.default405), Nothing})(req::HTTP.Messages.Request)
│        @ HTTP.Handlers ~/.julia/packages/HTTP/z8l0i/src/Handlers.jl:427
│      [8] requestHandler(req::HTTP.Messages.Request)
│        @ Main ./REPL[7]:2
│      [9] (::HTTP.Handlers.var"#1#2"{typeof(requestHandler)})(stream::HTTP.Streams.Stream{HTTP.Messages.Request, HTTP.ConnectionPool.Connection{Sockets.TCPSocket}})
│        @ HTTP.Handlers ~/.julia/packages/HTTP/z8l0i/src/Handlers.jl:58
│     [10] #invokelatest#2
│        @ ./essentials.jl:816 [inlined]
│     [11] invokelatest
│        @ ./essentials.jl:813 [inlined]
│     [12] handle_connection(f::Function, c::HTTP.ConnectionPool.Connection{Sockets.TCPSocket}, listener::HTTP.Servers.Listener{Nothing, Sockets.TCPServer}, readtimeout::Int64, access_log::Nothing)
│        @ HTTP.Servers ~/.julia/packages/HTTP/z8l0i/src/Servers.jl:447
│     [13] macro expansion
│        @ ~/.julia/packages/HTTP/z8l0i/src/Servers.jl:385 [inlined]
│     [14] (::HTTP.Servers.var"#16#17"{HTTP.Handlers.var"#1#2"{typeof(requestHandler)}, HTTP.Servers.Listener{Nothing, Sockets.TCPServer}, Set{HTTP.ConnectionPool.Connection}, Int64, Nothing, Base.Semaphore, HTTP.ConnectionPool.Connection{Sockets.TCPSocket}})()
│        @ HTTP.Servers ./task.jl:514
└ @ HTTP.Servers ~/.julia/packages/HTTP/z8l0i/src/Servers.jl:461

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions