Skip to content

proposal: add httputil.TCPKeepAliveListener #23378

@kevinburke

Description

@kevinburke

Currently there's no easy way for users to recreate the behavior
present in http.ListenAndServeTLS, short of copying the
tcpKeepAliveListener out of the net/http package. As a refresher, that's this:

func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
	addr := srv.Addr
	if addr == "" {
		addr = ":https"
	}

	ln, err := net.Listen("tcp", addr)
	if err != nil {
		return err
	}

	defer ln.Close()

	return srv.ServeTLS(tcpKeepAliveListener{ln.(*net.TCPListener)}, certFile, keyFile)
}

Making that package public would make it easy for people to mirror the behavior in that function. In my case, I like to open a socket, log a message, and then start the server, as the server blocks until shutdown.

It also seems like a lot of people are trying to copy it, scan the results here:
https://github.com/search?q=tcpKeepAliveListener+language%3Ago&ref=simplesearch&type=Code&utf8=%E2%9C%93

Two questions:

  • We can't load it in net/http because that would involve an import cycle. Is a copy okay?
  • the keep-alive period is hard coded to 3 minutes. Should we allow users to configure this? The zero value can conceivably be valid.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions