openresty / openresty/lua-nginx-module
Feature request: dynamic listen/close socket in lua
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 11.8k
- Forks
- 2.1k
- Avg merge
- 6h 1m
- Merged PRs (30d)
- 6
Description
Hi there,
I think many user need the new feature that we can dynamic listen or close a socket in the lua context, such as the discuss here, or some user' PR #1393 .
About the usage of this function, I have some ideas:
- The usage should like this:
function accept_hook(socket)
local b, err = sock:receive(8) -- like ngx.tcp.socket/ngx.udp.socket, it is a stream-typed cosocket
if err then
return
end
sock:send("welcome")
ngx.exit() -- close the socket for tcp
end
local err = ngx.socket.listen("tcp://127.0.0.1:5050", accept_hook) -- can not bind same address more than one time
if err then
ngx.log(ngx.ERR, "listen failed")
return
end
And support close the listen by the address, like this:
local err = ngx.socket.close("tcp://127.0.0.1:5050")
if err then
ngx.log(ngx.ERR, "listen failed")
return
end
-
ngx.socket.listenfor TCP, will bind, listen and accept a socket, and put the socekt into event list, like ngx_event_accept, and for UDP, will bind and recvmsg for the socket, like [ngx_event_recvmsg], and put the socekt into event list(https://github.com/nginx/nginx/blob/master/src/event/ngx_event_udp.c#L32). -
This should be like
ngx.timer, can use in each phase. If run it ininit_by_lua, all worker will bind same address by reuseport. -
Different worker can bind same address in different time by reuseport.
-
When the worker exit, all listen will close automatic.
-
A new independent phase should be build(like
timer, maybe name aslua_socket?), some function, such asngx.req.*can not be use.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the referenced NGINX entry points ngx_event_accept.c and ngx_event_udp.c, then inspect the existing ngx.timer and ngx.socket APIs in lua-nginx-module. Define how ngx.socket.listen and ngx.socket.close should work for TCP and UDP across worker phases, including init_by_lua and worker exit. Done means the behavior is implemented with coverage for binding, accepting or receiving, closing, reuseport, and lifecycle handling.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua, nginx
- Domain
- backend, networking
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100