Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 07:00 UTC
caddyhttp: Close http3 server gracefully (#6213)
* close http3 server gracefully * update server field * update from upstream --------- Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
WeidiDeng committed
Oct 16, 2024 at 09:28 UTC
c6f2979986d87d7236b132c687c8887c92248dd8
2 files changed
+2
-33
modules/caddyhttp/app.go
+1
-10
@@ -689,16 +689,7 @@ func (app *App) Stop() error {
689
return
690
}
691
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 {
693
app.logger.Error("HTTP/3 server shutdown",
694
zap.Error(err),
695
zap.Strings("addresses", server.Listen))
modules/caddyhttp/server.go
+1
-23
@@ -614,22 +614,7 @@ func (s *Server) serveHTTP3(addr caddy.NetworkAddress, tlsCfg *tls.Config) error
614
// create HTTP/3 server if not done already
615
if s.h3server == nil {
616
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,
618
TLSConfig: tlsCfg,
619
MaxHeaderBytes: s.MaxHeaderBytes,
620
QUICConfig: &quic.Config{
@@ -637,9 +622,6 @@ func (s *Server) serveHTTP3(addr caddy.NetworkAddress, tlsCfg *tls.Config) error
622
Tracer: qlog.DefaultConnectionTracer,
623
},
624
IdleTimeout: time.Duration(s.IdleTimeout),
640
- ConnContext: func(ctx context.Context, c quic.Connection) context.Context {
641
- return context.WithValue(ctx, quicConnCtxKey, c)
642
- },
625
}
626
}
627
@@ -1099,10 +1081,6 @@ const (
1081
// For referencing underlying net.Conn
1082
ConnCtxKey caddy.CtxKey = "conn"
1083
1102
- // For referencing underlying quic.Connection
1103
- // TODO: export if needed later
1104
- quicConnCtxKey caddy.CtxKey = "quic_conn"
1105
-
1084
// For tracking whether the client is a trusted proxy
1085
TrustedProxyVarKey string = "trusted_proxy"
1086