Skip to content

Commit 8056f2c

Browse files
vmgjulienschmidt
authored andcommitted
packets: reset read deadline before conn check (#964)
* packets: reset read deadline before conn check If a MySQL connection has been configured with a short `ReadTimeout`, each read from the TCP connection will be preceded by a `SetReadDeadline` call, which lingers until the next `SetReadDeadline`. This can be an issue if the connection becomes stale after staying too long in the connection pool, because when we attempt to perform a stale connection check, the Go runtime scheduler will return a timedout error from the scheduler itself, without letting us get to the kernel to perform the non-blocking read. To fix this, reset the read deadline before we perform the connection check. * packets: set a 0 deadline
1 parent d0a5481 commit 8056f2c

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

packets.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,17 @@ func (mc *mysqlConn) writePacket(data []byte) error {
108108
if mc.rawConn != nil {
109109
conn = mc.rawConn
110110
}
111-
if err := connCheck(conn); err != nil {
111+
var err error
112+
// If this connection has a ReadTimeout which we've been setting on
113+
// reads, reset it to its default value before we attempt a non-blocking
114+
// read, otherwise the scheduler will just time us out before we can read
115+
if mc.cfg.ReadTimeout != 0 {
116+
err = conn.SetReadDeadline(time.Time{})
117+
}
118+
if err == nil {
119+
err = connCheck(conn)
120+
}
121+
if err != nil {
112122
errLog.Print("closing bad idle connection: ", err)
113123
mc.Close()
114124
return driver.ErrBadConn

0 commit comments

Comments
 (0)