@@ -97,6 +97,7 @@ type Pool struct {
9797 maxConnLifetimeJitter time.Duration
9898 maxConnIdleTime time.Duration
9999 healthCheckPeriod time.Duration
100+ pingTimeout time.Duration
100101
101102 healthCheckMu sync.Mutex
102103 healthCheckTimer * time.Timer
@@ -169,6 +170,10 @@ type Config struct {
169170 // MaxConnIdleTime is the duration after which an idle connection will be automatically closed by the health check.
170171 MaxConnIdleTime time.Duration
171172
173+ // PingTimeout is the maximum amount of time to wait for a connection to pong before considering it as unhealthy and
174+ // destroying it. If zero, the default is no timeout.
175+ PingTimeout time.Duration
176+
172177 // MaxConns is the maximum size of the pool. The default is the greater of 4 or runtime.NumCPU().
173178 MaxConns int32
174179
@@ -241,6 +246,7 @@ func NewWithConfig(ctx context.Context, config *Config) (*Pool, error) {
241246 maxConnLifetime : config .MaxConnLifetime ,
242247 maxConnLifetimeJitter : config .MaxConnLifetimeJitter ,
243248 maxConnIdleTime : config .MaxConnIdleTime ,
249+ pingTimeout : config .PingTimeout ,
244250 healthCheckPeriod : config .HealthCheckPeriod ,
245251 healthCheckChan : make (chan struct {}, 1 ),
246252 closeChan : make (chan struct {}),
@@ -614,7 +620,14 @@ func (p *Pool) Acquire(ctx context.Context) (c *Conn, err error) {
614620
615621 shouldPingParams := ShouldPingParams {Conn : cr .conn , IdleDuration : res .IdleDuration ()}
616622 if p .shouldPing (ctx , shouldPingParams ) {
617- err := cr .conn .Ping (ctx )
623+ pingCtx := ctx
624+ if p .pingTimeout > 0 {
625+ var cancel context.CancelFunc
626+ pingCtx , cancel = context .WithTimeout (ctx , p .pingTimeout )
627+ defer cancel ()
628+ }
629+
630+ err := cr .conn .Ping (pingCtx )
618631 if err != nil {
619632 res .Destroy ()
620633 continue
0 commit comments