golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
121.06k stars 17.36k forks source link

proposal: net: export listenerBacklog #67990

Open database64128 opened 2 weeks ago

database64128 commented 2 weeks ago

Proposal Details

The listenerBacklog function returns a cached value of the optimal maximum permitted listener backlog on the current system, obtained in platform-specific ways. This is useful for user code that makes direct listen(2) syscalls and then calls net.FileListener to create a net.Listener.

Since the function signature is unlikely to change in the future, and we already had it exempted from the linkname restriction, I propose we add:

// MaxListenerBacklog returns the maximum permitted listener backlog value on the current system.
// The return value may be used as the backlog argument to the listen(2) syscall.
func MaxListenerBacklog() int
ianlancetaylor commented 2 weeks ago

I suggest a doc change. The point of this function in the net package is to determine the maximum permitted backlog argument, not the "optimal" backlog argument.

This function could easily be written as an external package. It doesn't really belong in the net package; as far as I know there are no functions in the net package that take a backlog argument. We could consider adding MaxListenerBacklog to the x/sys/unix package (there's not much point to adding it to x/sys/windows or x/sys/plan9, as on those systems there is nothing to look up).

database64128 commented 1 week ago

I suggest a doc change. The point of this function in the net package is to determine the maximum permitted backlog argument, not the "optimal" backlog argument.

Thanks. Updated per your suggestion.

This function could easily be written as an external package. It doesn't really belong in the net package; as far as I know there are no functions in the net package that take a backlog argument. We could consider adding MaxListenerBacklog to the x/sys/unix package (there's not much point to adding it to x/sys/windows or x/sys/plan9, as on those systems there is nothing to look up).

I would prefer to have this function available on all platforms, just like how the unexported one in net is, so it's easier for people to switch from linknaming against net.listenerBacklog. It would also match the signature of syscall.Listen.

But I guess only adding MaxListenerBacklog to x/sys/unix works for me too, as tfo-go only uses this on Linux.