How can I use send response to downstream and then use ngx.timer.at to send request to backend api(如何实现先返回给客户端,然后再异步请求更后端的服务呢)

75 views
Skip to first unread message

程序猿航仔

unread,
Jan 27, 2022, 10:21:30 PMJan 27
to openresty-en
Hi~  

We have a lua code that first reponse to the downstream through the ngx.eof , and then requests other api.  I learned the method from this article. https://moonbingbing.gitbooks.io/openresty-best-practices/content/ngx_lua/continue_after_eof.html

the code in nginx.conf

企业微信截图_20220128105820.png

the code in  proxy_pspeed_custom.lua

企业微信截图_20220128105929.png

we use   keepalive_timeout 0 and ngx.eof() to  response to downstream and  request to sub-request async.  Just like the picture show below:

1639053868-9008-61b1fa2cdbf06-545975.png

BUT,I learned from the article that it is better to use the zero delay of ngx.timer.at. (https://moonbingbing.gitbooks.io/openresty-best-practices/content/ngx_lua/timer.html) . Becaust although the terminal request has been returned, the worker of Nginx is still occupied by me.

AND  I think keepalive timeout 0 will cause the handshake to lose a lot of performance.

So I used ngx.timer.at to rebuild it.

Nginx.conf:
企业微信截图_20220128110707.png
proxy_pspeed_custom.lua
QQ浏览器截图20220128110808.png

However,
when I use  wrk to test the performance, I found that using ngx.timer.at QPS is even lower, which is not quite what I expected. I thought the performance would be better after opening keepalive and use ngx.timer.at.

On a 16-core 32G machine,

when using ngx.eof

企业微信截图_20220128111321.png
when using ngx.timer.at:
AND I found many TIME_WAIT request 
企业微信截图_20220128111321.png

It bothers me.


Need Help.

Thanks.

-------------------------------------------Translate to Chinese-----------------------------------------------------

我们是一个做日志上报的网关,我们希望通过 openresty 实现先返回 204 给客户端,然后再上报给更后端的服务。

我们之前使用 keepalive timeout + ngx.eof 实现,但是了解到 ngx.eof 以后还是会占用 worker,所以想通过 ngx.timer.at 实现,但是压测下来的结果不是很理想,不知道是不是我的用法不对。 请求大家指点指点。

感谢 

程序猿航仔

unread,
Feb 8, 2022, 4:30:25 AMFeb 8
to openresty-en
Correct some data : ngx.eof data update
企业微信截图_16432643916244.png

chengjie zhou

unread,
Feb 10, 2022, 5:45:00 AMFeb 10
to openre...@googlegroups.com
you can try this style:


local function sendToSpeedCustom()
-- send to other service
end

ngx.timer.at(0, sendToSpeedCustom)

ngx.status=204
ngx.say("eof")
return ngx.exit(200)




程序猿航仔 <johan...@gmail.com> 于2022年2月8日周二 17:30写道:
--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/openresty-en/5c6ab257-db7e-43e8-b3f2-111261d72293n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages