@@ -202,6 +202,22 @@ immutable HttpRpcServer{T,F}
202202 server:: Server
203203end
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+
205221HttpRpcServer {T,F} (api:: APIInvoker{T,F} , preproc:: Function = default_preproc) = HttpRpcServer ([api], preproc)
206222function 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)
0 commit comments