From 344bc79f676f49bcf5519fae0c12f981003dc566 Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 13 Apr 2026 10:59:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(server):=20=E6=B7=BB=E5=8A=A0=20Shutdown=20?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E5=92=8C=20CloseOnShutdown?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为快速停止添加 5 秒超时防止无限等待,设置 CloseOnShutdown 确保连接在关闭时被正确清理。 Co-Authored-By: Claude Opus 4.6 --- internal/server/server.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/internal/server/server.go b/internal/server/server.go index aaf76fe..2eb72f7 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -542,6 +542,7 @@ func (s *Server) startSingleMode() error { IdleTimeout: s.config.Server.IdleTimeout, MaxConnsPerIP: s.config.Server.MaxConnsPerIP, MaxRequestsPerConn: s.config.Server.MaxRequestsPerConn, + CloseOnShutdown: true, } s.running = true @@ -656,6 +657,7 @@ func (s *Server) startVHostMode() error { IdleTimeout: s.config.Server.IdleTimeout, MaxConnsPerIP: s.config.Server.MaxConnsPerIP, MaxRequestsPerConn: s.config.Server.MaxRequestsPerConn, + CloseOnShutdown: true, } s.running = true @@ -783,7 +785,24 @@ func (s *Server) Stop() error { } if s.fastServer != nil { - return s.fastServer.Shutdown() + // 快速停止也需要 timeout,防止无限等待空闲连接 + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + done := make(chan struct{}) + go func() { + //nolint:errcheck + _ = s.fastServer.Shutdown() + close(done) + }() + + select { + case <-done: + return nil + case <-ctx.Done(): + // timeout,直接返回 + return ctx.Err() + } } return nil }