Skip to content

Commit f118bb6

Browse files
committed
Attempt to acquire a connection MaxConns + 1 times before aborting
1 parent 4015a0c commit f118bb6

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

pgxpool/pool.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pgxpool
22

33
import (
44
"context"
5+
"errors"
56
"math/rand"
67
"runtime"
78
"strconv"
@@ -565,7 +566,10 @@ func (p *Pool) Acquire(ctx context.Context) (c *Conn, err error) {
565566
}()
566567
}
567568

568-
for {
569+
// Try to acquire from the connection pool up to maxConns + 1 times, so that
570+
// any that fatal errors would empty the pool and still at least try 1 fresh
571+
// connection.
572+
for range p.maxConns + 1 {
569573
res, err := p.p.Acquire(ctx)
570574
if err != nil {
571575
return nil, err
@@ -599,6 +603,7 @@ func (p *Pool) Acquire(ctx context.Context) (c *Conn, err error) {
599603

600604
return cr.getConn(p, res), nil
601605
}
606+
return nil, errors.New("pgxpool: detected infinite loop acquiring connection; likely bug in PrepareConn or BeforeAcquire hook")
602607
}
603608

604609
// AcquireFunc acquires a *Conn and calls f with that *Conn. ctx will only affect the Acquire. It has no effect on the

0 commit comments

Comments
 (0)