Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: net: fd and fdgram networks for net.Dial to connect to file descriptor numbers #69852

Open
MayCXC opened this issue Oct 11, 2024 · 2 comments
Labels
Milestone

Comments

@MayCXC
Copy link

MayCXC commented Oct 11, 2024

Proposal Details

I would like to use Dial like so:

net.Dial("fd", "3")

or ListenPacket like so:

net.ListenPacket("fdgram", "3")

to create a FilePacketConn from a file descriptor number. I would also like to use Listen like so:

net.Listen("fd", "0")

to create a FileListener from a file descriptor number. This is useful because many go programs read a network and address from some form of configuration, and then pass these values to net.Dial or net.Listen to connect or bind to the given address. It is desirable for these programs to be able to inherit file descriptors from a parent process for these connections and listeners, without making any changes made to their implementation, for at least two use cases:

  • programs can be run in a containerized environment with networking completely disabled but still use net for I/O
  • socket managers can terminate and respawn child servers that inherit listeners, without clients seeing an ECONNREFUSED

both of these use cases can already be implemented with NewFile, FileListener, and FilePacketConn, but adding support for them directly to Dial and Listen abstracts their implementation down to the level of configuration, which most programs using net implement already. For consistency, separate funcs with typed parameters similar to the other Dial* and Listen* funcs, can also be added:

  • func DialFd(network string, lfd, rfd uintptr) (FilePacketConn, error)
  • func ListenFd(network string, lfd uintptr) (FileListener, error)
  • func ListenFdgram(network string, lfd uintptr) (FilePacketConn, error)

all of these funcs can be implemented as simple compositions of NewFile with FileListener or FilePacketConn. I can add all of these features to net if there is support for them.

@gopherbot gopherbot added this to the Proposal milestone Oct 11, 2024
@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Oct 11, 2024
@ianlancetaylor
Copy link
Contributor

Does this permit anything that you can't already do using net.FileConn and net.FileListener with os.NewFile?

@MayCXC
Copy link
Author

MayCXC commented Oct 11, 2024

Does this permit anything that you can't already do using net.FileConn and net.FileListener with os.NewFile?

no, it just saves programs from having to implement separate branches with those funcs for file descriptors, and with net.Dial/net.Listen for the current networks. it is pretty much an upstream of this caddy contribution: https://github.com/caddyserver/caddy/blob/4b1a9b6cc1aa521e21289afa276d29952a97d8f3/listen.go#L97

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

3 participants