Skip to content

Commit b50e086

Browse files
committed
Avoid overflow when MaxConns is set to MaxInt32
fixes #2379
1 parent 67d7f3f commit b50e086

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

pgxpool/pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ func (p *Pool) Acquire(ctx context.Context) (c *Conn, err error) {
591591
// Try to acquire from the connection pool up to maxConns + 1 times, so that
592592
// any that fatal errors would empty the pool and still at least try 1 fresh
593593
// connection.
594-
for range p.maxConns + 1 {
594+
for range int(p.maxConns) + 1 {
595595
res, err := p.p.Acquire(ctx)
596596
if err != nil {
597597
return nil, err

pgxpool/pool_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"math"
78
"os"
89
"sync/atomic"
910
"testing"
@@ -245,6 +246,27 @@ func TestPoolAcquireChecksIdleConnsWithShouldPing(t *testing.T) {
245246
c.Release()
246247
}
247248

249+
// https://github.com/jackc/pgx/issues/2379
250+
func TestPoolAcquireWithMaxConnsEqualsMaxInt32(t *testing.T) {
251+
t.Parallel()
252+
253+
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
254+
defer cancel()
255+
256+
config, err := pgxpool.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
257+
require.NoError(t, err)
258+
259+
config.MaxConns = math.MaxInt32
260+
261+
pool, err := pgxpool.NewWithConfig(ctx, config)
262+
require.NoError(t, err)
263+
defer pool.Close()
264+
265+
c, err := pool.Acquire(ctx)
266+
require.NoError(t, err)
267+
c.Release()
268+
}
269+
248270
func TestPoolAcquireFunc(t *testing.T) {
249271
t.Parallel()
250272

0 commit comments

Comments
 (0)