Skip to content

Commit ca87ac6

Browse files
committed
dynamic reevaluation of function
Call `Core._apply_latest` to support `open` mode in `APIResponder` on Julia 0.6. We can call `Compat.invokelatest` instead after JuliaLang/julia#22646 is merged and assimilated in Compat.jl suitably.
1 parent 457dcd1 commit ca87ac6

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/APIResponder.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,23 @@ function get_resp(api::Nullable{APISpec}, status::Symbol, resp=nothing)
7878
end
7979
end
8080

81+
# Note: needs to be changed when https://github.com/JuliaLang/julia/pull/22646 is merged
82+
function dynamic_invoke(conn::APIResponder, f, args...; kwargs...)
83+
if conn.open && isdefined(Core, :_apply_latest)
84+
inner() = f(args...; kwargs...)
85+
Core._apply_latest(inner)
86+
else
87+
f(args...; kwargs...)
88+
end
89+
end
90+
8191
"""call the actual API method, and send the return value back as response"""
8292
function call_api(api::APISpec, conn::APIResponder, args, data::Dict{Symbol,Any})
8393
try
8494
if !applicable(api.fn, args...)
8595
narrow_args!(args)
8696
end
87-
result = api.fn(args...; data...)
97+
result = dynamic_invoke(conn, api.fn, args...; data...)
8898
respond(conn, Nullable(api), :success, result)
8999
catch ex
90100
err("api_exception: $ex")

test/srvr.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ const SRVR_ADDR = "tcp://127.0.0.1:9999"
1313
const JSON_RESP_HDRS = Dict{String,String}("Content-Type" => "application/json; charset=utf-8")
1414
const BINARY_RESP_HDRS = Dict{String,String}("Content-Type" => "application/octet-stream")
1515

16-
function run_srvr(fmt, tport, async=false)
16+
function run_srvr(fmt, tport, async=false, open=false)
1717
Logging.configure(level=INFO, filename="apisrvr_test.log")
1818
Logging.info("queue is at $SRVR_ADDR")
1919

20-
api = APIResponder(tport, fmt)
20+
api = APIResponder(tport, fmt, nothing, open)
2121
Logging.info("responding with: $api")
2222

2323
register(api, testfn1; resp_json=true, resp_headers=JSON_RESP_HDRS)
@@ -40,7 +40,7 @@ function test_preproc(req::Request, res::Response)
4040
end
4141

4242
function run_httprpcsrvr(fmt, tport, async=false)
43-
run_srvr(fmt, tport, true)
43+
run_srvr(fmt, tport, true, true)
4444
apiclnt = APIInvoker(ZMQTransport(SRVR_ADDR, REQ, false), fmt)
4545
if async
4646
@async run_http(apiclnt, 8888, test_preproc)

0 commit comments

Comments
 (0)