Skip to content

Commit

Permalink
fix ConnPool race in newConn
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Stotskiy committed Feb 7, 2024
1 parent 2512123 commit a45126a
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/pool/pool.go
Expand Up @@ -168,9 +168,12 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
return nil, ErrClosed
}

p.connsMu.Lock()
if p.cfg.MaxActiveConns > 0 && p.poolSize >= p.cfg.MaxActiveConns {
p.connsMu.Unlock()
return nil, ErrPoolExhausted
}
p.connsMu.Unlock()

cn, err := p.dialConn(ctx, pooled)
if err != nil {
Expand All @@ -180,6 +183,11 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
p.connsMu.Lock()
defer p.connsMu.Unlock()

if p.cfg.MaxActiveConns > 0 && p.poolSize >= p.cfg.MaxActiveConns {
_ = cn.Close()
return nil, ErrPoolExhausted
}

p.conns = append(p.conns, cn)
if pooled {
// If pool is full remove the cn on next Put.
Expand Down

0 comments on commit a45126a

Please sign in to comment.