Hello!
2014-11-23 17:34 GMT-08:00 唐坤:
> 我们经过压力测试发现, 如果record里面的代码阻塞(例如获取redis连接一直获取不到, 那么就会一直等待, 这里没加settimeout)
>
> 按理说 ngx.thread.spawn 不是异步的吗, 为什么会阻塞页面的显示呢~
>
引用一下文档:
https://github.com/openresty/lua-nginx-module#ngxthreadspawn
"By default, the corresponding Nginx handler (e.g., rewrite_by_lua
handler) will not terminate until
1. both the "entry thread" and all the user "light threads" terminates,
2. a "light thread" (either the "entry thread" or a user "light
thread" aborts by calling ngx.exit,ngx.exec, ngx.redirect, or
ngx.req.set_uri(uri, true), or
3. the "entry thread" terminates with a Lua error.
"
所以,按照条件 1,当前的请求处理程序在仍有用户轻线程活着的时候是不会终止的。
如果你想异步于当前的请求处理程序,则应当使用 ngx.timer.at 这个 API:
https://github.com/openresty/lua-nginx-module#ngxtimerat
值得一提的是,你应当尽量使用 redis 连接池以避免频繁地新建和断开到 redis 的连接,见
https://github.com/openresty/lua-resty-redis#set_keepalive
另外,建议加入 openresty 中文邮件列表讨论这样的问题,谢谢合作!见 http://openresty.org/#Community
Regards,
-agentzh
P.S. 同时抄送给 openresty 中文邮件列表。