Skip to content

Commit

Permalink
rebase to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
WeidiDeng committed Aug 18, 2022
1 parent dd769ad commit df54c0c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
23 changes: 13 additions & 10 deletions modules/caddyhttp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"strconv"
"sync"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package h2chandler
package caddyhttp

import (
"context"
Expand All @@ -11,23 +11,23 @@ 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,
}
}

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.
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions modules/caddyhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ type Server struct {
h3server *http3.Server
addresses []caddy.NetworkAddress

h2chandler *h2chandler

shutdownAt time.Time
shutdownAtMu *sync.RWMutex
}
Expand Down

0 comments on commit df54c0c

Please sign in to comment.