局部代码
response body 修改阶段向特定的API发起请求,不会阻塞正常响应,用于对响应速度要求极高、可靠性高、可容忍部分上报失败的场景
-- 构造post json body
local reduce_api = conf["response"].reduce_api
local request_body = string.format('{"fee": "%s"}', ngx.ctx.fee)
local request_args = {
method = "POST",
body = request_body,
headers = {
["Content-Type"] = "application/json"
}
}
-- 构造请求参数
local function send_reduce_request()
httpc:set_timeouts(3000,3000,3000)
local _, err = httpc:request_uri(reduce_api, request_args)
core.log.debug("==send request to reduce api. data: ", core.json.encode(request_args))
if err then
core.log.error("reduce api error: " .. err)
end
end
-- 构造异步请求
local ok, err = ngx.timer.at(0, send_reduce_request)
if not ok then
core.log.error("Failed to create timer: " .. err)
end