diff options
author | WeidiDeng <[email protected]> | 2024-03-30 01:51:46 +0800 |
---|---|---|
committer | GitHub <[email protected]> | 2024-03-29 11:51:46 -0600 |
commit | 924010cd3d1e0269cd89f3d7531e375ebbf11a3c (patch) | |
tree | 81bc3cbd3b83fe81915590015b90809de7572dec | |
parent | 74949fb0914d7d496efadf51ef2dd81e64b1b7d0 (diff) | |
download | caddy-924010cd3d1e0269cd89f3d7531e375ebbf11a3c.tar.gz caddy-924010cd3d1e0269cd89f3d7531e375ebbf11a3c.zip |
caddyhttp: close quic connections when server closes (#6202)
* close quic connections when server closes
* fix lint
* add comment about CloseGracefully
-rw-r--r-- | modules/caddyhttp/server.go | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go index ea748bc12..c7e5a5f61 100644 --- a/modules/caddyhttp/server.go +++ b/modules/caddyhttp/server.go @@ -568,12 +568,30 @@ func (s *Server) serveHTTP3(addr caddy.NetworkAddress, tlsCfg *tls.Config) error // create HTTP/3 server if not done already if s.h3server == nil { s.h3server = &http3.Server{ - Handler: s, + // Currently when closing a http3.Server, only listeners are closed. But caddy reuses these listeners + // if possible, requests are still read and handled by the old handler. Close these connections manually. + // see issue: https://github.com/caddyserver/caddy/issues/6195 + // Will interrupt ongoing requests. + // TODO: remove the handler wrap after http3.Server.CloseGracefully is implemented, see App.Stop + Handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + select { + case <-s.ctx.Done(): + if quicConn, ok := request.Context().Value(quicConnCtxKey).(quic.Connection); ok { + //nolint:errcheck + quicConn.CloseWithError(quic.ApplicationErrorCode(http3.ErrCodeRequestRejected), "") + } + default: + s.ServeHTTP(writer, request) + } + }), TLSConfig: tlsCfg, MaxHeaderBytes: s.MaxHeaderBytes, // TODO: remove this config when draft versions are no longer supported (we have no need to support drafts) QuicConfig: &quic.Config{ - Versions: []quic.VersionNumber{quic.Version1, quic.Version2}, + Versions: []quic.Version{quic.Version1, quic.Version2}, + }, + ConnContext: func(ctx context.Context, c quic.Connection) context.Context { + return context.WithValue(ctx, quicConnCtxKey, c) }, } } @@ -992,6 +1010,10 @@ const ( // For referencing underlying net.Conn ConnCtxKey caddy.CtxKey = "conn" + // For referencing underlying quic.Connection + // TODO: export if needed later + quicConnCtxKey caddy.CtxKey = "quic_conn" + // For tracking whether the client is a trusted proxy TrustedProxyVarKey string = "trusted_proxy" |