Skip to content

Commit 5aa430d

Browse files
committed
newConn race fix
1 parent 21bd40a commit 5aa430d

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

internal/pool/pool.go

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

166166
func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
167-
if p.closed() {
168-
return nil, ErrClosed
169-
}
170-
171-
if p.cfg.MaxActiveConns > 0 && p.poolSize >= p.cfg.MaxActiveConns {
172-
return nil, ErrPoolExhausted
173-
}
174-
175167
cn, err := p.dialConn(ctx, pooled)
176168
if err != nil {
177169
return nil, err
@@ -180,6 +172,18 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
180172
p.connsMu.Lock()
181173
defer p.connsMu.Unlock()
182174

175+
// It is not allowed to add new connections to the closed connection pool.
176+
if p.closed() {
177+
_ = cn.Close()
178+
return nil, ErrClosed
179+
}
180+
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+
183187
p.conns = append(p.conns, cn)
184188
if pooled {
185189
// If pool is full remove the cn on next Put.

0 commit comments

Comments
 (0)