Skip to content

Commit c62deef

Browse files
authored
Merge pull request #58 from JuliaWeb/tan/misc
allow async processing from httpserver
2 parents 57ffdf6 + 58a8a2e commit c62deef

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

src/http_rpc_server.jl

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function get_multipart_form_boundary(req::Request)
140140
parts[2]
141141
end
142142

143-
function http_handler(api::APIInvoker, preproc::Function, req::Request, res::Response)
143+
function http_handler{T,F}(apis::Channel{APIInvoker{T,F}}, preproc::Function, req::Request, res::Response)
144144
Logging.info("processing request ", req)
145145

146146
try
@@ -164,13 +164,19 @@ function http_handler(api::APIInvoker, preproc::Function, req::Request, res::Res
164164
res = Response(404)
165165
else
166166
cmd = shift!(args)
167-
if isempty(data_dict)
168-
Logging.debug("calling cmd ", cmd, ", with args ", args)
169-
res = httpresponse(api.format, apicall(api, cmd, args...))
170-
else
171-
vargs = make_vargs(data_dict)
172-
Logging.debug("calling cmd ", cmd, ", with args ", args, ", vargs ", vargs)
173-
res = httpresponse(api.format, apicall(api, cmd, args...; vargs...))
167+
Logging.info("waiting for a handler")
168+
api = take!(apis)
169+
try
170+
if isempty(data_dict)
171+
Logging.debug("calling cmd ", cmd, ", with args ", args)
172+
res = httpresponse(api.format, apicall(api, cmd, args...))
173+
else
174+
vargs = make_vargs(data_dict)
175+
Logging.debug("calling cmd ", cmd, ", with args ", args, ", vargs ", vargs)
176+
res = httpresponse(api.format, apicall(api, cmd, args...; vargs...))
177+
end
178+
finally
179+
put!(apis, api)
174180
end
175181
end
176182
end
@@ -190,29 +196,33 @@ on_listen(port) = Logging.info("listening on port ", port, "...")
190196
default_preproc(req::Request, res::Response) = true
191197

192198
# add a multipart form handler, provide default
193-
type HttpRpcServer
194-
api::APIInvoker
199+
immutable HttpRpcServer{T,F}
200+
api::Channel{APIInvoker{T,F}}
195201
handler::HttpHandler
196202
server::Server
203+
end
197204

198-
function HttpRpcServer(api::APIInvoker, preproc::Function=default_preproc)
199-
r = new()
200-
201-
function handler(req::Request, res::Response)
202-
return http_handler(api, preproc, req, res)
203-
end
205+
HttpRpcServer{T,F}(api::APIInvoker{T,F}, preproc::Function=default_preproc) = HttpRpcServer([api], preproc)
206+
function HttpRpcServer{T,F}(apis::Vector{APIInvoker{T,F}}, preproc::Function=default_preproc)
207+
api = Channel{APIInvoker{T,F}}(length(apis))
208+
for member in apis
209+
put!(api, member)
210+
end
204211

205-
r.api = api
206-
r.handler = HttpHandler(handler)
207-
r.handler.events["error"] = on_error
208-
r.handler.events["listen"] = on_listen
209-
r.server = Server(r.handler)
210-
r
212+
function handler(req::Request, res::Response)
213+
return http_handler(api, preproc, req, res)
211214
end
215+
216+
handler = HttpHandler(handler)
217+
handler.events["error"] = on_error
218+
handler.events["listen"] = on_listen
219+
server = Server(handler)
220+
221+
HttpRpcServer{T,F}(api, handler, server)
212222
end
213223

214-
run_http(api::APIInvoker, port::Int, preproc::Function=default_preproc) = run_http(api, preproc; port=port)
215-
function run_http(api::APIInvoker, preproc::Function=default_preproc; kwargs...)
224+
run_http{T,F}(api::Union{Vector{APIInvoker{T,F}},APIInvoker{T,F}}, port::Int, preproc::Function=default_preproc) = run_http(api, preproc; port=port)
225+
function run_http{T,F}(api::Union{Vector{APIInvoker{T,F}},APIInvoker{T,F}}, preproc::Function=default_preproc; kwargs...)
216226
Logging.debug("running HTTP RPC server...")
217227
httprpc = HttpRpcServer(api, preproc)
218228
run(httprpc.server; kwargs...)

0 commit comments

Comments
 (0)