diff --git a/src/http_rpc_server.jl b/src/http_rpc_server.jl index b985c50..35c5ccb 100644 --- a/src/http_rpc_server.jl +++ b/src/http_rpc_server.jl @@ -202,6 +202,22 @@ immutable HttpRpcServer{T,F} server::Server end +function reusable_tcpserver() + tcp = Base.TCPServer(Base.Libc.malloc(Base._sizeof_uv_tcp), Base.StatusUninit) + err = ccall(:uv_tcp_init_ex, Cint, (Ptr{Void}, Ptr{Void}, Cuint), + Base.eventloop(), tcp.handle, 2) + Base.uv_error("failed to create tcp server", err) + tcp.status = Base.StatusInit + + rc = ccall(:jl_tcp_reuseport, Int32, (Ptr{Void},), tcp.handle) + if rc == 0 + Logging.info("Reusing TCP port") + else + Logging.warn("Unable to reuse TCP port, error:", rc) + end + return tcp +end + HttpRpcServer{T,F}(api::APIInvoker{T,F}, preproc::Function=default_preproc) = HttpRpcServer([api], preproc) function HttpRpcServer{T,F}(apis::Vector{APIInvoker{T,F}}, preproc::Function=default_preproc) api = Channel{APIInvoker{T,F}}(length(apis)) @@ -213,7 +229,7 @@ function HttpRpcServer{T,F}(apis::Vector{APIInvoker{T,F}}, preproc::Function=def return http_handler(api, preproc, req, res) end - handler = HttpHandler(handler) + handler = HttpHandler(handler, reusable_tcpserver()) handler.events["error"] = on_error handler.events["listen"] = on_listen server = Server(handler) diff --git a/test/clnt.jl b/test/clnt.jl index 3c6ca1d..bc02d07 100644 --- a/test/clnt.jl +++ b/test/clnt.jl @@ -6,6 +6,7 @@ using Logging using ZMQ using Base.Test using Requests +using JSON Logging.configure(level=INFO) diff --git a/test/srvr.jl b/test/srvr.jl index 9895ae6..a2c0d7e 100644 --- a/test/srvr.jl +++ b/test/srvr.jl @@ -6,6 +6,7 @@ using HttpCommon using Logging using Compat using ZMQ +using JSON include("srvrfn.jl")