Przeglądaj źródła

http2: initialize Server.IdleTimeout from http.Server as http1 does

This makes ConfigureServer initialize the http2 Server's IdleTimeout
from the http1 Server configuration, using the same rules as
IdleTimeout in Go 1.8: first use IdleTimeout if specified, else fall
back to the old ReadTimeout.

Updates golang/go#14204

Change-Id: I4dee971f8416ef0cbf99335a199c46355f9ab09d
Reviewed-on: https://go-review.googlesource.com/32230
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Tom Bergan <tombergan@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Brad Fitzpatrick 9 lat temu
rodzic
commit
b336a971b7
2 zmienionych plików z 31 dodań i 6 usunięć
  1. 15 0
      http2/go18.go
  2. 16 6
      http2/server.go

+ 15 - 0
http2/go18.go

@@ -24,3 +24,18 @@ func (w *responseWriter) Push(target string, opts *http.PushOptions) error {
 	}
 	}
 	return w.push(target, internalOpts)
 	return w.push(target, internalOpts)
 }
 }
+
+func init() {
+	configServerFuncs = append(configServerFuncs, configureServer18)
+}
+
+func configureServer18(h1 *http.Server, h2 *Server) error {
+	if h2.IdleTimeout == 0 {
+		if h1.IdleTimeout != 0 {
+			h2.IdleTimeout = h1.IdleTimeout
+		} else {
+			h2.IdleTimeout = h1.ReadTimeout
+		}
+	}
+	return nil
+}

+ 16 - 6
http2/server.go

@@ -129,15 +129,27 @@ func (s *Server) maxConcurrentStreams() uint32 {
 	return defaultMaxStreams
 	return defaultMaxStreams
 }
 }
 
 
+// List of funcs for ConfigureServer to run. Both h1 and h2 are guaranteed
+// to be non-nil.
+var configServerFuncs []func(h1 *http.Server, h2 *Server) error
+
 // ConfigureServer adds HTTP/2 support to a net/http Server.
 // ConfigureServer adds HTTP/2 support to a net/http Server.
 //
 //
 // The configuration conf may be nil.
 // The configuration conf may be nil.
 //
 //
 // ConfigureServer must be called before s begins serving.
 // ConfigureServer must be called before s begins serving.
 func ConfigureServer(s *http.Server, conf *Server) error {
 func ConfigureServer(s *http.Server, conf *Server) error {
+	if s == nil {
+		panic("nil *http.Server")
+	}
 	if conf == nil {
 	if conf == nil {
 		conf = new(Server)
 		conf = new(Server)
 	}
 	}
+	for _, fn := range configServerFuncs {
+		if err := fn(s, conf); err != nil {
+			return err
+		}
+	}
 
 
 	if s.TLSConfig == nil {
 	if s.TLSConfig == nil {
 		s.TLSConfig = new(tls.Config)
 		s.TLSConfig = new(tls.Config)
@@ -1544,12 +1556,10 @@ func (sc *serverConn) processHeaders(f *MetaHeadersFrame) error {
 	// The net/http package sets the read deadline from the
 	// The net/http package sets the read deadline from the
 	// http.Server.ReadTimeout during the TLS handshake, but then
 	// http.Server.ReadTimeout during the TLS handshake, but then
 	// passes the connection off to us with the deadline already
 	// passes the connection off to us with the deadline already
-	// set. Disarm it here after the request headers are read, similar
-	// to how the http1 server works.
-	// Unlike http1, though, we never re-arm it yet, though.
-	// TODO(bradfitz): figure out golang.org/issue/14204
-	// (IdleTimeout) and how this relates. Maybe the default
-	// IdleTimeout is ReadTimeout.
+	// set. Disarm it here after the request headers are read,
+	// similar to how the http1 server works. Here it's
+	// technically more like the http1 Server's ReadHeaderTimeout
+	// (in Go 1.8), though. That's a more sane option anyway.
 	if sc.hs.ReadTimeout != 0 {
 	if sc.hs.ReadTimeout != 0 {
 		sc.conn.SetReadDeadline(time.Time{})
 		sc.conn.SetReadDeadline(time.Time{})
 	}
 	}