Skip to content

Commit 6e4e3e7

Browse files
committed
Merge pull request #2619 from 1991santhu/fix/pool-ping-timeout-connstring
pgxpool: accept pool_ping_timeout in the connection string
2 parents 0aebf61 + 3846117 commit 6e4e3e7

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

pgxpool/pool.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var (
2222
defaultMaxConnLifetime = time.Hour
2323
defaultMaxConnIdleTime = time.Minute * 30
2424
defaultHealthCheckPeriod = time.Minute
25+
defaultPingTimeout = time.Duration(0)
2526
)
2627

2728
type connResource struct {
@@ -344,10 +345,12 @@ func NewWithConfig(ctx context.Context, config *Config) (*Pool, error) {
344345
//
345346
// - pool_max_conns: integer greater than 0 (default 4)
346347
// - pool_min_conns: integer 0 or greater (default 0)
348+
// - pool_min_idle_conns: integer 0 or greater (default 0)
347349
// - pool_max_conn_lifetime: duration string (default 1 hour)
348350
// - pool_max_conn_idle_time: duration string (default 30 minutes)
349351
// - pool_health_check_period: duration string (default 1 minute)
350352
// - pool_max_conn_lifetime_jitter: duration string (default 0)
353+
// - pool_ping_timeout: duration string (default 0, meaning no timeout)
351354
//
352355
// See Config for definitions of these arguments.
353356
//
@@ -448,6 +451,17 @@ func ParseConfig(connString string) (*Config, error) {
448451
config.MaxConnLifetimeJitter = d
449452
}
450453

454+
if s, ok := config.ConnConfig.Config.RuntimeParams["pool_ping_timeout"]; ok {
455+
delete(connConfig.Config.RuntimeParams, "pool_ping_timeout")
456+
d, err := time.ParseDuration(s)
457+
if err != nil {
458+
return nil, pgconn.NewParseConfigError(connString, "cannot parse pool_ping_timeout", err)
459+
}
460+
config.PingTimeout = d
461+
} else {
462+
config.PingTimeout = defaultPingTimeout
463+
}
464+
451465
return config, nil
452466
}
453467

pgxpool/pool_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,28 @@ func TestConnectConfig(t *testing.T) {
4444
func TestParseConfigExtractsPoolArguments(t *testing.T) {
4545
t.Parallel()
4646

47-
config, err := pgxpool.ParseConfig("pool_max_conns=42 pool_min_conns=1 pool_min_idle_conns=2")
47+
config, err := pgxpool.ParseConfig("pool_max_conns=42 pool_min_conns=1 pool_min_idle_conns=2 pool_ping_timeout=250ms")
4848
assert.NoError(t, err)
4949
assert.EqualValues(t, 42, config.MaxConns)
5050
assert.EqualValues(t, 1, config.MinConns)
5151
assert.EqualValues(t, 2, config.MinIdleConns)
52+
assert.Equal(t, 250*time.Millisecond, config.PingTimeout)
53+
54+
// Anything left in RuntimeParams is sent to the server as a startup parameter, which makes every connection fail
55+
// with "unrecognized configuration parameter".
5256
assert.NotContains(t, config.ConnConfig.Config.RuntimeParams, "pool_max_conns")
5357
assert.NotContains(t, config.ConnConfig.Config.RuntimeParams, "pool_min_conns")
58+
assert.NotContains(t, config.ConnConfig.Config.RuntimeParams, "pool_min_idle_conns")
59+
assert.NotContains(t, config.ConnConfig.Config.RuntimeParams, "pool_ping_timeout")
60+
61+
config, err = pgxpool.ParseConfig("")
62+
assert.NoError(t, err)
63+
assert.Zero(t, config.PingTimeout)
64+
65+
for _, v := range []string{"abc", "250"} {
66+
_, err := pgxpool.ParseConfig("pool_ping_timeout=" + v)
67+
assert.Errorf(t, err, "pool_ping_timeout=%s should be rejected", v)
68+
}
5469
}
5570

5671
func TestConstructorIgnoresContext(t *testing.T) {

0 commit comments

Comments
 (0)