middleware: reuse inet_diag netlink socket
commit4dcd2caa9d7691d6a4f1d3fc0a0a4b54ac6fad6b
authorEric Wong <bofh@yhbt.net>
Fri, 29 Dec 2023 17:44:18 +0000 (29 17:44 +0000)
committerEric Wong <bofh@yhbt.net>
Mon, 15 Jan 2024 02:57:56 +0000 (15 02:57 +0000)
tree91d02013d44889dcd71607c653b87baf3d6b5d0d
parente2dfea8b2c74d94b66efc00c2ccab3df7af750a5
middleware: reuse inet_diag netlink socket

Eric Wong <bofh@yhbt.net> wrote:
> I'll squash this in for fork+preload safety.

I forget there are multithreaded servers using this :x

------8<-----
Subject: [PATCH v3] middleware: reuse inet_diag netlink socket

No point in constantly allocating and deallocating FDs (and Ruby
IO objects) when reusing them is supported.

However, we must guard it against parallel callers since
linux_inet_diag.c::diag releases the GVL while calling
sendmsg(2) and recvmsg(2), so it's possible two Ruby threads can
end up crossing streams if the middlware is used with a
multi-threaded server.

AFAIK, Raindrops::Middleware isn't a heavily-trafficked endpoint,
so saving FDs and avoiding thread specific data is preferable
for memory-constrained systems I use.
lib/raindrops/middleware.rb