【问题标题】:Openresty concurrent requestsOpenresty 并发请求
【发布时间】:2017-02-20 13:52:45
【问题描述】:

我想将 OpenResty 与 Lua 解释器一起使用。

我无法让 OpenResty 框架处理对两个不同端点的两个并发请求。我模拟了一个请求通过在一个长循环中运行来进行一些艰苦的计算:

local function busyWaiting()
    local self = coroutine.running()
    local i = 1
    while i < 9999999 do
        i = i + 1
        coroutine.yield(self)
    end
end

local self = coroutine.running()
local thread = ngx.thread.spawn(busyWaiting)

while (coroutine.status(thread) ~= 'zombie') do
    coroutine.yield(self)
end


ngx.say('test1!')

另一个端点只是立即发送响应。 ngx.say('test2')

我向第一个端点发送请求,然后向第二个端点发送第二个请求。但是,OpenResty 被第一个请求阻止了,所以我几乎同时收到了两个响应。

将 nginx 参数 worker_processes 1; 设置为更高的数字也无济于事,无论如何我希望只有一个工作进程。

让 OpenResty 处理额外请求而不被第一个请求阻塞的正确方法是什么?

【问题讨论】:

  • 您没有提供任何代码向我们展示您如何发送子请求。我假设您使用 ngx.location.capture 之类的东西将子请求发送到端点。对于并行运行的子请求,您应该使用 github.com/openresty/lua-nginx-module#ngxlocationcapture_multi API。
  • 我没有使用 ngx.location.capture。我使用两个单独的客户端连接到同一服务器上的两个不同端点。
  • @JeFi 抱歉,没看懂你的用例,现在明白了,看下面我的回答
  • 你解决过这个问题吗?
  • @MappaM ,不幸的是我没有。

标签: multithreading nginx lua openresty


【解决方案1】:
local function busyWaiting()
    local self = ngx.coroutine.running()
    local i = 1
    while i < 9999999 do
        i = i + 1
        ngx.coroutine.yield(self)
    end
end

local thread = ngx.thread.spawn(busyWaiting)

while (ngx.coroutine.status(thread) ~= 'dead') do
    ngx.coroutine.resume(thread)
end


ngx.say('test1!')

【讨论】:

  • 在第二个 while 循环中将 coroutine.yield 更改为 coroutine.resume 不会导致解释器继续使用协程递增变量 i。变量i 仅递增到值 2。然后第二个 while 循环变为无限。
  • 对不起,你在 Openresty 环境下工作,添加“ngx”。所有 API 的前缀
  • ngx.coroutine 不存在,这个答案并没有解决问题...
  • @MappaM 你很有趣,github.com/openresty/…
  • 您应该在回答中解释您所做的更改以及您认为它应该起作用的原因。也许是因为我尝试使用 NGINX+Lua 的标准版本,而不是 Openresty 的那个。其中协程是主包名。 ngx.coroutine 不存在并给出错误。而且使用 coroutine.yield 并不能解决手头的问题,即使屈服,服务器也不会处理另一个请求。
猜你喜欢
  • 1970-01-01
  • 2018-08-25
  • 2020-01-27
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多