Parcourir la source

Use Golang's default keep-alive.

Since Go 1.13 a net.Listen keep-alive is enabled by default if
the protocol and OS supports it. The new one is 15s to match
the net.Dial default one. Previously http.Server ListenAndServe
and ListenAndServeTLS used to add a wrapper with 3m that we
replicated.

See https://github.com/golang/go/issues/31510
Mariano Cano il y a 5 ans
Parent
commit
36b622bfc2
3 fichiers modifiés avec 4 ajouts et 23 suppressions
  1. 2 2
      ca/tls.go
  2. 0 1
      examples/basic-client/client.go
  3. 2 20
      server/server.go

+ 2 - 2
ca/tls.go

@@ -279,9 +279,9 @@ func getDefaultTLSConfig(sign *api.SignResponse) *tls.Config {
 
 
 // getDefaultDialer returns a new dialer with the default configuration.
 // getDefaultDialer returns a new dialer with the default configuration.
 func getDefaultDialer() *net.Dialer {
 func getDefaultDialer() *net.Dialer {
+	// With the KeepAlive parameter set to 0, it will be use Golang's default.
 	return &net.Dialer{
 	return &net.Dialer{
-		Timeout:   30 * time.Second,
-		KeepAlive: 30 * time.Second,
+		Timeout: 30 * time.Second,
 	}
 	}
 }
 }
 
 

+ 0 - 1
examples/basic-client/client.go

@@ -116,7 +116,6 @@ func main() {
 			Proxy: http.ProxyFromEnvironment,
 			Proxy: http.ProxyFromEnvironment,
 			DialContext: (&net.Dialer{
 			DialContext: (&net.Dialer{
 				Timeout:   30 * time.Second,
 				Timeout:   30 * time.Second,
-				KeepAlive: 30 * time.Second,
 				DualStack: true,
 				DualStack: true,
 			}).DialContext,
 			}).DialContext,
 			MaxIdleConns:          100,
 			MaxIdleConns:          100,

+ 2 - 20
server/server.go

@@ -72,10 +72,10 @@ func (srv *Server) Serve(ln net.Listener) error {
 		// Start server
 		// Start server
 		if srv.TLSConfig == nil || (len(srv.TLSConfig.Certificates) == 0 && srv.TLSConfig.GetCertificate == nil) {
 		if srv.TLSConfig == nil || (len(srv.TLSConfig.Certificates) == 0 && srv.TLSConfig.GetCertificate == nil) {
 			log.Printf("Serving HTTP on %s ...", srv.Addr)
 			log.Printf("Serving HTTP on %s ...", srv.Addr)
-			err = srv.Server.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})
+			err = srv.Server.Serve(ln)
 		} else {
 		} else {
 			log.Printf("Serving HTTPS on %s ...", srv.Addr)
 			log.Printf("Serving HTTPS on %s ...", srv.Addr)
-			err = srv.Server.ServeTLS(tcpKeepAliveListener{ln.(*net.TCPListener)}, "", "")
+			err = srv.Server.ServeTLS(ln, "", "")
 		}
 		}
 
 
 		// log unexpected errors
 		// log unexpected errors
@@ -155,21 +155,3 @@ func (srv *Server) Forbidden(w http.ResponseWriter) {
 	w.WriteHeader(http.StatusForbidden)
 	w.WriteHeader(http.StatusForbidden)
 	w.Write([]byte("Forbidden.\n"))
 	w.Write([]byte("Forbidden.\n"))
 }
 }
-
-// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
-// connections. It's used by ListenAndServe and ListenAndServeTLS so
-// dead TCP connections (e.g. closing laptop mid-download) eventually
-// go away.
-type tcpKeepAliveListener struct {
-	*net.TCPListener
-}
-
-func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
-	tc, err := ln.AcceptTCP()
-	if err != nil {
-		return
-	}
-	tc.SetKeepAlive(true)
-	tc.SetKeepAlivePeriod(3 * time.Minute)
-	return tc, nil
-}