Skip to content

Commit

Permalink
stdlib: Use Ping instead of CheckConn in ResetSession
Browse files Browse the repository at this point in the history
CheckConn is deprecated. It doesn't detect all network outages. It
causes a 1ms delay while it tries to read the connection. Ping incurs a
round trip but that means it is a much stronger guarantee that the
connection is usable. In addition, if the application and the database
are on the same network it will actually be faster as round trip times
are typically a few hundred microseconds.
  • Loading branch information
jackc committed Oct 27, 2023
1 parent 7a4bb7e commit cf6ef75
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion stdlib/sql.go
Expand Up @@ -548,7 +548,7 @@ func (c *Conn) ResetSession(ctx context.Context) error {

now := time.Now()
if now.Sub(c.lastResetSessionTime) > time.Second {
if err := c.conn.PgConn().CheckConn(); err != nil {
if err := c.conn.PgConn().Ping(ctx); err != nil {
return driver.ErrBadConn
}
}
Expand Down

0 comments on commit cf6ef75

Please sign in to comment.