reusing udp sockets

263 views
Skip to first unread message

li...@die-jansens.de

unread,
Feb 10, 2015, 3:20:39 AM2/10/15
to openre...@googlegroups.com
Hi,

I'm trying my first steps with nginx and lua, so maybe I'm just doing
it completely wrong...
I want to use a udp socket in content_by_lua. Just opening one, using
it and closing it afterwards works fine, but to save resources I want
to reuse the socket for multiple requests. So instead of freeing it
after the request, I put it in a pool (local variable in my module).
In the next request, I take the socket from the pool, but get the error
"attempt to send data on a closed socket". So it seems the lua-variable
is still there and has not fallen prey to the GC.
The question is, how can I keep the socket open for the next request?

Thanks, Arne

Lord Nynex

unread,
Feb 10, 2015, 1:18:33 PM2/10/15
to openre...@googlegroups.com
Hello,

ngx.socket.udp does not appear to have a :setkeepalive() method like ngx.socket.tcp. agentzh, is this correct?


--
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.
For more options, visit https://groups.google.com/d/optout.

Yichun Zhang (agentzh)

unread,
Feb 10, 2015, 3:00:50 PM2/10/15
to openresty-en
Hi

On Tue, Feb 10, 2015 at 10:18 AM, Lord Nynex wrote:
> ngx.socket.udp does not appear to have a :setkeepalive() method like
> ngx.socket.tcp. agentzh, is this correct?
>

Yes, correct. Volunteers welcome and patches welcome :)

Regards,
-agentzh

Brian Akins

unread,
Feb 11, 2015, 7:00:42 PM2/11/15
to openre...@googlegroups.com
What would be "kept alive" in a UDP socket? Some memory allocations, etc?

Yichun Zhang (agentzh)

unread,
Feb 11, 2015, 7:14:34 PM2/11/15
to openresty-en
Hello!

On Wed, Feb 11, 2015 at 4:00 PM, Brian Akins wrote:
> What would be "kept alive" in a UDP socket? Some memory allocations, etc?
>

More importantly, it can save the (expensive) socket(), ioctl(), and
close() syscalls for the UDP/datagram socket upon every request.

Regards,
-agentzh

li...@die-jansens.de

unread,
Feb 12, 2015, 2:42:57 AM2/12/15
to openre...@googlegroups.com
So I take it that I currently can't do that. What I'm wondering about is
why the socket gets closed in the first place, even though the variable
doesn't go out of scope. What is the reasoning behind this?

Thanks, Arne

>
> Regards,
> -agentzh
>

Yichun Zhang (agentzh)

unread,
Feb 12, 2015, 3:29:02 PM2/12/15
to openresty-en
Hello!

On Wed, Feb 11, 2015 at 11:42 PM, li...@die-jansens.de wrote:
>
> So I take it that I currently can't do that. What I'm wondering about is
> why the socket gets closed in the first place, even though the variable
> doesn't go out of scope. What is the reasoning behind this?
>

According to the current implementation, cosocket objects' lifetime
and scope cannot go across the creating request handler's boundary.
One has to rely on ngx_lua's builtin connection/socket pools to reuse
the underlying connections/sockets. This design decision eliminates
many (if not all) programming mistakes by incorrectly sharing cosocket
objects across different nginx requests and it also simplifies
resource management.

Regards,
-agentzh

Nicolas Adrian

unread,
Apr 27, 2022, 7:24:17 AM4/27/22
to openresty-en
Hello,

Resurrecting this thread.

Is there any plan to introduce UDP socket reuse ?
Do I still have to create a ngx.socket.udp object for each request ?

Regards,
Nicolas

reflection_probe

unread,
Apr 27, 2022, 7:25:27 AM4/27/22
to openre...@googlegroups.com
UDP does not have sockets so I don't see any reason to reuse anything.
unless you mean the actual cosocket instances themselves.

Nicolas Adrian

unread,
Apr 27, 2022, 7:30:17 AM4/27/22
to openresty-en
> UDP does not have sockets so I don't see any reason to reuse anything.
> unless you mean the actual cosocket instances themselves.

Yes I do mean the actual coscket instance

Nicolas Adrian

unread,
Apr 27, 2022, 8:18:45 AM4/27/22
to openresty-en
>> UDP does not have sockets so I don't see any reason to reuse anything.
>> unless you mean the actual cosocket instances themselves.

> Yes I do mean the actual coscket instance

So can I reuse the object accross requests ?

Junlong li

unread,
Apr 28, 2022, 8:41:54 AM4/28/22
to openresty-en
You can not reuse the object at the moment.

Nicolas Adrian

unread,
Jun 7, 2022, 4:39:26 AM6/7/22
to openresty-en
Will it be implemented in the near future ? My goal in my use case would be to save some syscalls
Reply all
Reply to author
Forward
0 new messages