From 1d40c61c196d650f03e0914c701a5014a4049f96 Mon Sep 17 00:00:00 2001 From: Jiajia Zhong Date: Sun, 9 Jan 2022 00:54:47 +0800 Subject: [PATCH] Fixed SetReadDeadline before connCheck there's no need to set conn's readline if CheckConnLiveness is false, and the ReadTimeout shall work with rawConn.Read inside conncheck. buffer.fill will do SetReadDeadline if needed --- packets.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packets.go b/packets.go index 1867ecab2..ab30601ae 100644 --- a/packets.go +++ b/packets.go @@ -110,14 +110,13 @@ func (mc *mysqlConn) writePacket(data []byte) error { conn = mc.rawConn } var err error - // If this connection has a ReadTimeout which we've been setting on - // reads, reset it to its default value before we attempt a non-blocking - // read, otherwise the scheduler will just time us out before we can read - if mc.cfg.ReadTimeout != 0 { - err = conn.SetReadDeadline(time.Time{}) - } - if err == nil && mc.cfg.CheckConnLiveness { - err = connCheck(conn) + if mc.cfg.CheckConnLiveness { + if mc.cfg.ReadTimeout != 0 { + err = conn.SetReadDeadline(time.Now().Add(mc.cfg.ReadTimeout)) + } + if err == nil { + err = connCheck(conn) + } } if err != nil { errLog.Print("closing bad idle connection: ", err)