How to run Lua code when a client timed out?

226 views
Skip to first unread message

Andrew Bate

unread,
Nov 1, 2021, 5:19:04 PM11/1/21
to openresty-en
Hello all,

Is there any way to run Lua code when a client times out?

I have tried different directives including log_by_lua_block, but this only
runs if the client does not time out.

For example, if I add

log_by_lua_block {
    print("This message is from log_by_lua.")
}

to my Nginx configuration, the above message is only printed to the error log 
when the client completes its request.

If the client timed out, this message is not printed to the error log. I
know that the client did time out because in the error log there is an entry such as:

Nov 01 20:55:16 debian-a nginx[3277]: debian-a nginx: 2021/11/01 20:55:16 [info] 3277#3277: *3 client timed out (110: Connection timed out) while waiting for request, client: 192.168.80.1, server: 0.0.0.0:80

Is there a way to run Lua code on client time out?

Your help is appreciated!

weiping he

unread,
Nov 2, 2021, 7:37:25 AM11/2/21
to openre...@googlegroups.com
by "timeout", what do you mean? is it openresty to upstream timeout, or a client visit to openresty and timeouted?



Andrew Bate <ma...@andrewbate.com> 于2021年11月1日周一 下午9:19写道:
--
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/4584abb9-1a09-496e-8b5b-5be7c459a34bn%40googlegroups.com.

Andrew Bate

unread,
Nov 2, 2021, 7:42:40 AM11/2/21
to openre...@googlegroups.com
A client visiting OpenResty and timing out.

Andrew Bate

unread,
Nov 2, 2021, 1:17:14 PM11/2/21
to openresty-en
A similar question would be: Is there any way to run code when there is a read notification on the listening socket using OpenResty (i.e. the Nginx HTTP initialization phase)?

In Nginx this is handled by ngx_http_init_connection (see the Nginx development guide for details).

You can run a block of Lua code when a worker starts using init_worker_by_lua_block, or when a request begins with (for example) init_worker_by_lua_block. But there seems to be no way to run Lua code on initialization of the HTTP connection.

(This relates to my original question of how to run Lua code when a downstream client times out because looking at the source code of ngx_http_init_connection I can see that the timeout timers are started there.)

Any suggestions would be appreciated.

James LIAOHJ

unread,
Nov 7, 2021, 8:00:25 PM11/7/21
to openresty-en
-- define the code for your client timeout in a function
local function onClientTimeout()
    -- run your lua code
    ngx.exit(444)
end

-- registe the function you difined
local ok, err = ngx.on_abort(onClientTimeout)

Andrew Bate

unread,
Nov 7, 2021, 8:07:18 PM11/7/21
to openresty-en
Unfortunately that doesn't work because ngx.on_abort can only be called from rewrite_by_lua*, access_by_lua*, or content_by_lua*, and those contents only run after the client has finished sending the request.

I am looking to run code when a client's initial GET/POST request times out (e.g. the client does not sent all headers in time), not when the client closes the connection.

I've done tracing (printing to the log) from  rewrite_by_lua*, access_by_lua*, and content_by_lua*, and these contexts are not run if the client's request times out.

Reply all
Reply to author
Forward
0 new messages