From df54c0c694c3ee99f1d62b332229feab9bc62628 Mon Sep 17 00:00:00 2001 From: Weidi Deng Date: Thu, 18 Aug 2022 16:16:38 +0800 Subject: [PATCH] rebase to latest --- modules/caddyhttp/app.go | 23 +++++++++++-------- .../{h2chandler/handler.go => h2chandler.go} | 16 ++++++------- modules/caddyhttp/server.go | 2 ++ 3 files changed, 23 insertions(+), 18 deletions(-) rename modules/caddyhttp/{h2chandler/handler.go => h2chandler.go} (81%) diff --git a/modules/caddyhttp/app.go b/modules/caddyhttp/app.go index 429e8ebdc6f..a01f9ee4b92 100644 --- a/modules/caddyhttp/app.go +++ b/modules/caddyhttp/app.go @@ -18,7 +18,6 @@ import ( "context" "crypto/tls" "fmt" - "net" "net/http" "strconv" "sync" @@ -364,7 +363,11 @@ func (app *App) Start() error { h2server := &http2.Server{ NewWriteScheduler: func() http2.WriteScheduler { return http2.NewPriorityWriteScheduler(nil) }, } - srv.server.Handler = h2c.NewHandler(srv, h2server) + //nolint:errcheck + http2.ConfigureServer(srv.server, h2server) + h2chandler := newH2cHandler(h2c.NewHandler(srv, h2server)) + srv.server.Handler = h2chandler + srv.h2chandler = h2chandler } for _, lnAddr := range srv.Listen { @@ -502,14 +505,6 @@ func (app *App) Stop() error { zap.Error(err), zap.Strings("addresses", server.Listen)) } - } - for i, s := range app.h2chandlers { - if err := s.Shutdown(ctx); err != nil { - app.logger.Error("h2c handler shutdown", - zap.Error(err), - zap.Int("index", i)) - } - } if server.h3server != nil { // TODO: CloseGracefully, once implemented upstream (see https://github.com/lucas-clemente/quic-go/issues/2103) @@ -519,6 +514,14 @@ func (app *App) Stop() error { zap.Strings("addresses", server.Listen)) } } + + if server.h2chandler != nil { + if err := server.h2chandler.Shutdown(ctx); err != nil { + app.logger.Error("h2c handler shutdown", + zap.Error(err), + zap.Strings("addresses", server.Listen)) + } + } } return nil diff --git a/modules/caddyhttp/h2chandler/handler.go b/modules/caddyhttp/h2chandler.go similarity index 81% rename from modules/caddyhttp/h2chandler/handler.go rename to modules/caddyhttp/h2chandler.go index 6b709fa00c1..7d638fc4bca 100644 --- a/modules/caddyhttp/h2chandler/handler.go +++ b/modules/caddyhttp/h2chandler.go @@ -1,4 +1,4 @@ -package h2chandler +package caddyhttp import ( "context" @@ -11,15 +11,15 @@ import ( "golang.org/x/net/http/httpguts" ) -// Handler is a Handler which counts possible h2c upgrade requests -type Handler struct { +// h2chandler is a Handler which counts possible h2c upgrade requests +type h2chandler struct { cnt uint64 Handler http.Handler } -// NewHandler returns an http.Handler that tracks possible h2c upgrade requests. -func NewHandler(h http.Handler) *Handler { - return &Handler{ +// NewH2cHandler returns an http.Handler that tracks possible h2c upgrade requests. +func newH2cHandler(h http.Handler) *h2chandler { + return &h2chandler{ Handler: h, } } @@ -27,7 +27,7 @@ func NewHandler(h http.Handler) *Handler { const shutdownPollIntervalMax = 500 * time.Millisecond // Shutdown mirrors stdlib http.Server Shutdown behavior, because h2 connections are always marked active, there is no closing to be done. -func (h *Handler) Shutdown(ctx context.Context) error { +func (h *h2chandler) Shutdown(ctx context.Context) error { pollIntervalBase := time.Millisecond nextPollInterval := func() time.Duration { // Add 10% jitter. @@ -70,7 +70,7 @@ func isH2cUpgrade(r *http.Request) bool { } // ServeHTTP records underlying connections that are likely to be h2c. -func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (h *h2chandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if isH2cUpgrade(r) { atomic.AddUint64(&h.cnt, 1) defer atomic.AddUint64(&h.cnt, ^uint64(0)) diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go index f8cd612278a..5c783d51e10 100644 --- a/modules/caddyhttp/server.go +++ b/modules/caddyhttp/server.go @@ -162,6 +162,8 @@ type Server struct { h3server *http3.Server addresses []caddy.NetworkAddress + h2chandler *h2chandler + shutdownAt time.Time shutdownAtMu *sync.RWMutex }