uSockets icon indicating copy to clipboard operation
uSockets copied to clipboard

Add support for already-listening sockets

Open shivak opened this issue 3 years ago • 6 comments

Background. It's often desirable to work off a given listening fd, rather than opening a port or UNIX domain socket. For example, a listening FD can be passed from a process in a global network namespace to a server in a restricted namespace, for security/isolation. In systemd socket activation, the externally-facing network or domain socket is bound and listened by systemd. When a new connection arrives, the service is started with the socket descriptor passed as an int in the LISTEN_FDS environment variable.

Implementation. us_socket_context_listen_direct hastily added without tests or examples. Should probably be renamed, since it takes a listening socket, but doesn't call listen() itself. Just want to get this ball rolling, in the hopes that this mode is eventually supported by Bun. With node, it's possible to run servers directly in this manner. cc @Jarred-Sumner

shivak avatar Jan 05 '23 23:01 shivak

Just want to get this ball rolling, in the hopes that this mode is eventually supported by Bun. With node, it's possible to run servers directly in this manner. cc @Jarred-Sumner

This would be nice. The user or API one layer up would need to ensure that the file descriptor isn't already being polled by the current epoll or else it would cause strange behavior, but that's fine

Jarred-Sumner avatar Jan 06 '23 01:01 Jarred-Sumner

Smart, will have a look when I have time

uNetworkingAB avatar Jan 06 '23 09:01 uNetworkingAB

It looks small 👍

uNetworkingAB avatar Jan 06 '23 09:01 uNetworkingAB

Yup, just a matter of naming and sanitizing a long-term interface.

I added a small example of how this would be used with network namespaces.

shivak avatar Jan 06 '23 19:01 shivak

@uNetworkingAB, any chance of merging soon? Afterwards, it will be straightforward to add an fd option to Bun's ServeOptions. (Note the similar feature request in Deno: https://github.com/denoland/deno/discussions/14008)

shivak avatar May 01 '23 18:05 shivak

@e3dio , I tested systemd socket activation with the entire stack of changes to Bun and uSockets, since that was the purpose of the PRs. But in the last year, some minor conflicts have arisen. I don’t have time to resolve them right now, but feel free to pick up where I left off.

shivak avatar May 31 '24 01:05 shivak