不要把每个请求都会生成的数据保存在模块中。
不要用self.key = ngx.var.arg_key来操作。
可以考虑这种方式:
-- For simple singleshot requests, use the URI interface.
local http = require "resty.http"
local httpc = http.new()
local res, err = httpc:request_uri("http://example.com/helloworld", {
method = "POST",
body = "a=1&b=2",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
}
})
在 2017年1月6日星期五 UTC+8上午7:22:11,Jason Gian写道:
写了一个小框架概述如下:
配置文件:
init_by_lua_block {
ring = require 'foo'
}
access_by_lua_block {
ring.access("bar")
}
简化lua代码:
foo.lua
local ring = {}
local function run_plugins(plugin)--{{{
--for loop
local status, res = pcall(require, plugin)
if ret then
local status = res:run()
end
end
--}}}
function ring.access(...)--{{{
run_plugins({...})
end
--}}}
bar.lua
local _M = {}
function _M:t2(arg)
ngx.say(self.key)
ngx.exit(200)
end
function _M:t1()
self:t2("hello")
end
function _M:run()
self.key = ngx.var.arg_key
self:t1()
end
return _M
在2k 并发的情况下,同时请求
127.0.0.1/test?key=123 和
127.0.0.1/test?key=abc 传入key参数和不传入key参数
输出结果会有小概率交叉的情况,即:
添加日志打印能更方便验证
这里我估摸着是,这个bar.lua返回的这个table被多个请求(process)交叉污染了
请问有人知道如何解决吗?不让这个table被多个请求交叉污染