@@ -124,6 +124,22 @@ func init() {
124124// OptionOpenDB options for configuring the driver when opening a new db pool.
125125type OptionOpenDB func (* connector )
126126
127+ // ShouldPingParams are passed to OptionShouldPing to decide whether to ping before reusing a connection.
128+ type ShouldPingParams struct {
129+ // Conn is the underlying pgx connection.
130+ Conn * pgx.Conn
131+ // IdleDuration is how long it has been since ResetSession last ran.
132+ IdleDuration time.Duration
133+ }
134+
135+ // OptionShouldPing controls whether stdlib should issue a liveness ping before reusing a connection.
136+ // If the function returns true, stdlib will ping.
137+ // If it returns false, stdlib will skip the ping.
138+ // If not provided, default is ping only when IdleDuration > 1s.
139+ func OptionShouldPing (f func (context.Context , ShouldPingParams ) bool ) OptionOpenDB {
140+ return func (dc * connector ) { dc .ShouldPing = f }
141+ }
142+
127143// OptionBeforeConnect provides a callback for before connect. It is passed a shallow copy of the ConnConfig that will
128144// be used to connect, so only its immediate members should be modified. Used only if db is opened with *pgx.ConnConfig.
129145func OptionBeforeConnect (bc func (context.Context , * pgx.ConnConfig ) error ) OptionOpenDB {
@@ -231,6 +247,7 @@ type connector struct {
231247 BeforeConnect func (context.Context , * pgx.ConnConfig ) error // function to call before creation of every new connection
232248 AfterConnect func (context.Context , * pgx.Conn ) error // function to call after creation of every new connection
233249 ResetSession func (context.Context , * pgx.Conn ) error // function is called before a connection is reused
250+ ShouldPing func (context.Context , ShouldPingParams ) bool // function to decide if stdlib should ping before reusing a connection
234251 driver * Driver
235252}
236253
@@ -282,6 +299,7 @@ func (c connector) Connect(ctx context.Context) (driver.Conn, error) {
282299 driver : c .driver ,
283300 connConfig : connConfig ,
284301 resetSessionFunc : c .ResetSession ,
302+ shouldPing : c .ShouldPing ,
285303 psRefCounts : make (map [* pgconn.StatementDescription ]int ),
286304 }, nil
287305}
@@ -389,7 +407,8 @@ type Conn struct {
389407 close func (context.Context ) error
390408 driver * Driver
391409 connConfig pgx.ConnConfig
392- resetSessionFunc func (context.Context , * pgx.Conn ) error // Function is called before a connection is reused
410+ resetSessionFunc func (context.Context , * pgx.Conn ) error // Function is called before a connection is reused
411+ shouldPing func (context.Context , ShouldPingParams ) bool // Function to decide if stdlib should ping before reusing a connection
393412 lastResetSessionTime time.Time
394413
395414 // psRefCounts contains reference counts for prepared statements. Prepare uses the underlying pgx logic to generate
@@ -537,11 +556,23 @@ func (c *Conn) ResetSession(ctx context.Context) error {
537556 }
538557
539558 now := time .Now ()
540- if now .Sub (c .lastResetSessionTime ) > time .Second {
559+ idle := now .Sub (c .lastResetSessionTime )
560+
561+ doPing := idle > time .Second // default behavior: ping only if idle > 1s
562+
563+ if c .shouldPing != nil {
564+ doPing = c .shouldPing (ctx , ShouldPingParams {
565+ Conn : c .conn ,
566+ IdleDuration : idle ,
567+ })
568+ }
569+
570+ if doPing {
541571 if err := c .conn .PgConn ().Ping (ctx ); err != nil {
542572 return driver .ErrBadConn
543573 }
544574 }
575+
545576 c .lastResetSessionTime = now
546577
547578 return c .resetSessionFunc (ctx , c .conn )
0 commit comments