net/http: server.ListenAndServe should honour SetKeepAlivesEnabled #18427
Comments
/cc @bradfitz |
You're conflating two different types of keep-alives. The docs distinguish between the two by saying "TCP keep-alives" vs "HTTP keep-alives". The method you cite is about TCP keep-alives:
It has no control over TCP keep-alives as designed. You can pass in your own TCP listener if you want to modify the default ListenAndServe behavior. |
Merry Chrismas! @bradfitz: Thanks, I understand. Is there any benefit though to enabling TCP keep-alives when we know that no HTTP keep-alives will be performed? Surely that's unnecessary overhead on each connection that can be avoided? |
Thanks for your issue. |
Calling http.ListenAndServe will do a
net.Listen(...)
and then wrap that TCPListener in atcpKeepAliveListener
before calling Serve to handle any incoming connections.This is clear from the docs on ListenAndServe, which states that:
But, http.Server also provides the handy SetKeepAlivesEnabled method. So, it would seem to me that calling
SetKeepAlivesEnabled(false)
and thenListenAndServe()
would do the obvious thing and disable keep-alives on that server's connections.... which it seems to do — sending Connection: close, and likely closing it off after writing the response — however...
As far as I can tell the tcpKeepAliveListener will still always call
SetKeepAlive(true)
andSetKeepAlivePeriod(3 * time.Minute)
on each new connection, triggering a bunch ofsyscall.SetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, boolint(keepalive))
magic each time.Which can be avoided, especially if I'm disabling keep-alives before I even start serving my traffic.
So at the very least, this could be as simple as something like:
And doing the same on ListenAndServeTLS.
I haven't looked how feasible it would be to have the tcpKeepAliveListener itself stop setting socket keep-alives once the server setting is changed, but I guess that would be even better?
Am I missing something? Let me know what you think.
What version of Go are you using (
go version
)?go version go1.7.4 darwin/amd64
What operating system and processor architecture are you using (
go env
)?What did you do?
The text was updated successfully, but these errors were encountered: