fix: unify shutdown resource cleanup

This commit is contained in:
MiMoCode
2026-07-10 18:52:00 +08:00
parent 6fe0a8415d
commit 1b60c8df33
3 changed files with 145 additions and 35 deletions
+17 -7
View File
@@ -209,13 +209,23 @@ func (p *Pool) Get() clickhouse.Conn {
return p.conn
}
// Close 释放底层连接。
func (p *Pool) Close() {
// Close starts closing the underlying connection and waits within ctx.
func (p *Pool) Close(ctx context.Context) error {
p.mu.Lock()
defer p.mu.Unlock()
if p.conn != nil {
_ = p.conn.Close()
p.conn = nil
}
conn := p.conn
p.conn = nil
p.healthy = false
p.mu.Unlock()
if conn == nil {
return nil
}
done := make(chan error, 1)
go func() { done <- conn.Close() }()
select {
case err := <-done:
return err
case <-ctx.Done():
return ctx.Err()
}
}