Skip to content

Commit d822721

Browse files
committed
newConn race fix
1 parent 5aa430d commit d822721

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

internal/pool/pool.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ func (p *ConnPool) NewConn(ctx context.Context) (*Conn, error) {
164164
}
165165

166166
func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
167+
var poolExhausted bool
168+
169+
p.connsMu.Lock()
170+
if p.cfg.MaxActiveConns > 0 {
171+
poolExhausted = p.poolSize >= p.cfg.MaxActiveConns
172+
}
173+
p.connsMu.Unlock()
174+
175+
if poolExhausted {
176+
return nil, ErrPoolExhausted
177+
}
178+
167179
cn, err := p.dialConn(ctx, pooled)
168180
if err != nil {
169181
return nil, err
@@ -178,12 +190,6 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
178190
return nil, ErrClosed
179191
}
180192

181-
// It is not allowed to add new connections to the connection pool.
182-
if p.cfg.MaxActiveConns > 0 && p.poolSize >= p.cfg.MaxActiveConns {
183-
_ = cn.Close()
184-
return nil, ErrPoolExhausted
185-
}
186-
187193
p.conns = append(p.conns, cn)
188194
if pooled {
189195
// If pool is full remove the cn on next Put.

0 commit comments

Comments
 (0)