Skip to content

Commit c6f2979

Browse files
WeidiDengmholt
andauthored
caddyhttp: Close http3 server gracefully (#6213)
* close http3 server gracefully * update server field * update from upstream --------- Co-authored-by: Matt Holt <[email protected]>
1 parent a211c65 commit c6f2979

File tree

2 files changed

+2
-33
lines changed

2 files changed

+2
-33
lines changed

modules/caddyhttp/app.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -689,16 +689,7 @@ func (app *App) Stop() error {
689689
return
690690
}
691691

692-
// First close h3server then close listeners unlike stdlib for several reasons:
693-
// 1, udp has only a single socket, once closed, no more data can be read and
694-
// written. In contrast, closing tcp listeners won't affect established connections.
695-
// This have something to do with graceful shutdown when upstream implements it.
696-
// 2, h3server will only close listeners it's registered (quic listeners). Closing
697-
// listener first and these listeners maybe unregistered thus won't be closed. caddy
698-
// distinguishes quic-listener and underlying datagram sockets.
699-
700-
// TODO: CloseGracefully, once implemented upstream (see https://github.com/quic-go/quic-go/issues/2103)
701-
if err := server.h3server.Close(); err != nil {
692+
if err := server.h3server.Shutdown(ctx); err != nil {
702693
app.logger.Error("HTTP/3 server shutdown",
703694
zap.Error(err),
704695
zap.Strings("addresses", server.Listen))

modules/caddyhttp/server.go

+1-23
Original file line numberDiff line numberDiff line change
@@ -614,32 +614,14 @@ func (s *Server) serveHTTP3(addr caddy.NetworkAddress, tlsCfg *tls.Config) error
614614
// create HTTP/3 server if not done already
615615
if s.h3server == nil {
616616
s.h3server = &http3.Server{
617-
// Currently when closing a http3.Server, only listeners are closed. But caddy reuses these listeners
618-
// if possible, requests are still read and handled by the old handler. Close these connections manually.
619-
// see issue: https://github.com/caddyserver/caddy/issues/6195
620-
// Will interrupt ongoing requests.
621-
// TODO: remove the handler wrap after http3.Server.CloseGracefully is implemented, see App.Stop
622-
Handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
623-
select {
624-
case <-s.ctx.Done():
625-
if quicConn, ok := request.Context().Value(quicConnCtxKey).(quic.Connection); ok {
626-
//nolint:errcheck
627-
quicConn.CloseWithError(quic.ApplicationErrorCode(http3.ErrCodeRequestRejected), "")
628-
}
629-
default:
630-
s.ServeHTTP(writer, request)
631-
}
632-
}),
617+
Handler: s,
633618
TLSConfig: tlsCfg,
634619
MaxHeaderBytes: s.MaxHeaderBytes,
635620
QUICConfig: &quic.Config{
636621
Versions: []quic.Version{quic.Version1, quic.Version2},
637622
Tracer: qlog.DefaultConnectionTracer,
638623
},
639624
IdleTimeout: time.Duration(s.IdleTimeout),
640-
ConnContext: func(ctx context.Context, c quic.Connection) context.Context {
641-
return context.WithValue(ctx, quicConnCtxKey, c)
642-
},
643625
}
644626
}
645627

@@ -1099,10 +1081,6 @@ const (
10991081
// For referencing underlying net.Conn
11001082
ConnCtxKey caddy.CtxKey = "conn"
11011083

1102-
// For referencing underlying quic.Connection
1103-
// TODO: export if needed later
1104-
quicConnCtxKey caddy.CtxKey = "quic_conn"
1105-
11061084
// For tracking whether the client is a trusted proxy
11071085
TrustedProxyVarKey string = "trusted_proxy"
11081086

0 commit comments

Comments
 (0)