fix(server): 添加 Shutdown 超时和 CloseOnShutdown

为快速停止添加 5 秒超时防止无限等待,设置 CloseOnShutdown
确保连接在关闭时被正确清理。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-13 10:59:07 +08:00
parent e9d747a628
commit 344bc79f67

View File

@ -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
}