Eager initialize minpoolsize on connect
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user