Skip to content

Commit 83e0776

Browse files
committed
allow port reuse on HTTP end
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.
1 parent 3cebb14 commit 83e0776

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/http_rpc_server.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ immutable HttpRpcServer{T,F}
202202
server::Server
203203
end
204204

205+
function reusable_tcpserver()
206+
tcp = Base.TCPServer(Base.Libc.malloc(Base._sizeof_uv_tcp), Base.StatusUninit)
207+
err = ccall(:uv_tcp_init_ex, Cint, (Ptr{Void}, Ptr{Void}, Cuint),
208+
Base.eventloop(), tcp.handle, 2)
209+
Base.uv_error("failed to create tcp server", err)
210+
tcp.status = Base.StatusInit
211+
212+
rc = ccall(:jl_tcp_reuseport, Int32, (Ptr{Void},), tcp.handle)
213+
if rc == 0
214+
Logging.info("Reusing TCP port")
215+
else
216+
Logging.warn("Unable to reuse TCP port, error:", rc)
217+
end
218+
return tcp
219+
end
220+
205221
HttpRpcServer{T,F}(api::APIInvoker{T,F}, preproc::Function=default_preproc) = HttpRpcServer([api], preproc)
206222
function HttpRpcServer{T,F}(apis::Vector{APIInvoker{T,F}}, preproc::Function=default_preproc)
207223
api = Channel{APIInvoker{T,F}}(length(apis))
@@ -213,7 +229,7 @@ function HttpRpcServer{T,F}(apis::Vector{APIInvoker{T,F}}, preproc::Function=def
213229
return http_handler(api, preproc, req, res)
214230
end
215231

216-
handler = HttpHandler(handler)
232+
handler = HttpHandler(handler, reusable_tcpserver())
217233
handler.events["error"] = on_error
218234
handler.events["listen"] = on_listen
219235
server = Server(handler)

test/clnt.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using Logging
66
using ZMQ
77
using Base.Test
88
using Requests
9+
using JSON
910

1011
Logging.configure(level=INFO)
1112

test/srvr.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using HttpCommon
66
using Logging
77
using Compat
88
using ZMQ
9+
using JSON
910

1011
include("srvrfn.jl")
1112

0 commit comments

Comments
 (0)