@marcel91200 / caddy Mirror · read-only / commits / 924010cd
Fork 0

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, 14:30 UTC

caddyhttp: close quic connections when server closes (#6202)

* close quic connections when server closes * fix lint * add comment about CloseGracefully

WeidiDeng committed Mar 30, 2024 at 01:51 UTC 924010cd3d1e0269cd89f3d7531e375ebbf11a3c
1 file changed +24 -2
modules/caddyhttp/server.go
+24 -2
@@ -568,12 +568,30 @@ func (s *Server) serveHTTP3(addr caddy.NetworkAddress, tlsCfg *tls.Config) error
568 // create HTTP/3 server if not done already
569 if s.h3server == nil {
570 s.h3server = &http3.Server{
571 - Handler: s,
571 + // Currently when closing a http3.Server, only listeners are closed. But caddy reuses these listeners
572 + // if possible, requests are still read and handled by the old handler. Close these connections manually.
573 + // see issue: https://github.com/caddyserver/caddy/issues/6195
574 + // Will interrupt ongoing requests.
575 + // TODO: remove the handler wrap after http3.Server.CloseGracefully is implemented, see App.Stop
576 + Handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
577 + select {
578 + case <-s.ctx.Done():
579 + if quicConn, ok := request.Context().Value(quicConnCtxKey).(quic.Connection); ok {
580 + //nolint:errcheck
581 + quicConn.CloseWithError(quic.ApplicationErrorCode(http3.ErrCodeRequestRejected), "")
582 + }
583 + default:
584 + s.ServeHTTP(writer, request)
585 + }
586 + }),
587 TLSConfig: tlsCfg,
588 MaxHeaderBytes: s.MaxHeaderBytes,
589 // TODO: remove this config when draft versions are no longer supported (we have no need to support drafts)
590 QuicConfig: &quic.Config{
576 - Versions: []quic.VersionNumber{quic.Version1, quic.Version2},
591 + Versions: []quic.Version{quic.Version1, quic.Version2},
592 + },
593 + ConnContext: func(ctx context.Context, c quic.Connection) context.Context {
594 + return context.WithValue(ctx, quicConnCtxKey, c)
595 },
596 }
597 }
@@ -992,6 +1010,10 @@ const (
1010 // For referencing underlying net.Conn
1011 ConnCtxKey caddy.CtxKey = "conn"
1012
1013 + // For referencing underlying quic.Connection
1014 + // TODO: export if needed later
1015 + quicConnCtxKey caddy.CtxKey = "quic_conn"
1016 +
1017 // For tracking whether the client is a trusted proxy
1018 TrustedProxyVarKey string = "trusted_proxy"
1019