2
0

Fix goroutine leak and unclosed connections

This commit is contained in:
Daniel
2021-10-29 14:07:46 +02:00
committed by Jack Christensen
parent 36708a1cc6
commit 8bc6aa6b49
2 changed files with 83 additions and 4 deletions
+8 -4
View File
@@ -226,6 +226,8 @@ func ConnectConfig(ctx context.Context, config *Config) (*Pool, error) {
if !config.LazyConnect {
if err := p.createIdleResources(ctx, int(p.minConns)); err != nil {
// Couldn't create resources for minpool size. Close unhealthy pool.
p.Close()
return nil, err
}
@@ -385,7 +387,7 @@ func (p *Pool) createIdleResources(parentCtx context.Context, targetResources in
ctx, cancel := context.WithCancel(parentCtx)
defer cancel()
errs := make(chan error)
errs := make(chan error, targetResources)
for i := 0; i < targetResources; i++ {
go func() {
@@ -394,14 +396,16 @@ func (p *Pool) createIdleResources(parentCtx context.Context, targetResources in
}()
}
var firstError error
for i := 0; i < targetResources; i++ {
if err := <-errs; err != nil {
err := <-errs
if err != nil && firstError == nil {
cancel()
return err
firstError = err
}
}
return nil
return firstError
}
// Acquire returns a connection (*Conn) from the Pool