Is there any asynchronous alternative to ngx.location.capture?

91 views
Skip to first unread message

Damian Kamiński

unread,
Sep 22, 2017, 9:25:34 AM9/22/17
to openresty-en
Hi all,

According to lua nginx module documentation ngx.location.capture is non-blocking, but synchronous. In our case we would like to a send request without affecting latency therefore synchronous operation is not an option. We thought that maybe lua-resty-http module allows to do it, but after quick investigation it seems that it's using tcpsock:send which is synchronous.

Is there any asynchronous alternative?

Many thanks,
Damian.

Jim Robinson

unread,
Sep 22, 2017, 1:24:31 PM9/22/17
to openresty-en
Apologies if I'm missing something but the lua-resty-http code I see uses ngx.socket.tcp for self.sock, which I believe is nonblocking.

Yichun Zhang (agentzh)

unread,
Sep 23, 2017, 2:07:23 PM9/23/17
to openresty-en
Hello!

On Fri, Sep 22, 2017 at 6:25 AM, Damian Kamiński wrote:
\> According to lua nginx module documentation ngx.location.capture is
> non-blocking, but synchronous. In our case we would like to a send request
> without affecting latency therefore synchronous operation is not an option.
> We thought that maybe lua-resty-http module allows to do it, but after quick
> investigation it seems that it's using tcpsock:send which is synchronous.
>

If you do not want to add latency to the request handler, then you can
use the ngx.timer.at() API to create a (0-delay) timer that is
detached from the current request handler context. And yeah, you'll
need to use a lua-resty-http library that is based on our cosocket
API.

But be careful when the client sends request too fast, which may
accumulate your async timers VERY quickly. So a better solution might
be use a single chain of timer and batch the tasks in a queue (could
just be a Lua table) or something. You can check
lua-resty-logger-socket library for such an example.

Regards,
Yichun

Damian Kamiński

unread,
Sep 26, 2017, 7:46:36 AM9/26/17
to openresty-en
Timer and queue will work for us. Thanks.

Damian.
Reply all
Reply to author
Forward
0 new messages