2
0

Eager initialize minpoolsize on connect

This commit is contained in:
Daniel
2021-10-03 21:53:22 +02:00
committed by Jack Christensen
parent 044ba47522
commit 36708a1cc6
2 changed files with 129 additions and 0 deletions
+27
View File
@@ -225,6 +225,10 @@ func ConnectConfig(ctx context.Context, config *Config) (*Pool, error) {
go p.backgroundHealthCheck()
if !config.LazyConnect {
if err := p.createIdleResources(ctx, int(p.minConns)); err != nil {
return nil, err
}
// Initially establish one connection
res, err := p.p.Acquire(ctx)
if err != nil {
@@ -377,6 +381,29 @@ func (p *Pool) checkMinConns() {
}
}
func (p *Pool) createIdleResources(parentCtx context.Context, targetResources int) error {
ctx, cancel := context.WithCancel(parentCtx)
defer cancel()
errs := make(chan error)
for i := 0; i < targetResources; i++ {
go func() {
err := p.p.CreateResource(ctx)
errs <- err
}()
}
for i := 0; i < targetResources; i++ {
if err := <-errs; err != nil {
cancel()
return err
}
}
return nil
}
// Acquire returns a connection (*Conn) from the Pool
func (p *Pool) Acquire(ctx context.Context) (*Conn, error) {
for {