Skip to content

Commit

Permalink
allow port reuse on HTTP end
Browse files Browse the repository at this point in the history
This will allow multiple HTTP servers to be multiplexed on the same port on the HTTP end.
This is useful to bypass the limitation in Julia where a `ccall` blocks all tasks in the process.
With this change multiple HTTP clients can get requests serviced simultaneously using more than one servers.
The HTTP ends can further talk to one or more backends as needed.
  • Loading branch information
tanmaykm committed Oct 16, 2017
1 parent 3cebb14 commit 83e0776
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/http_rpc_server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/clnt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Logging
using ZMQ
using Base.Test
using Requests
using JSON

Logging.configure(level=INFO)

Expand Down
1 change: 1 addition & 0 deletions test/srvr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using HttpCommon
using Logging
using Compat
using ZMQ
using JSON

include("srvrfn.jl")

Expand Down

0 comments on commit 83e0776

Please sign in to comment.