From 9a555817e50c9e0fdacb40ea66413dcc6d4e922d Mon Sep 17 00:00:00 2001 From: ZhiFeng Hu Date: Sat, 12 Dec 2015 15:59:03 +0800 Subject: [PATCH] Add deadline for connection Signed-off-by: ZhiFeng Hu --- driver.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/driver.go b/driver.go index d310624ad..7b53d316d 100644 --- a/driver.go +++ b/driver.go @@ -20,6 +20,7 @@ import ( "database/sql" "database/sql/driver" "net" + "time" ) // This struct is exported to make the driver directly accessible. @@ -71,6 +72,9 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) { // Enable TCP Keepalives on TCP connections if tc, ok := mc.netConn.(*net.TCPConn); ok { + timeout := time.Now().Add(mc.cfg.timeout) + tc.SetReadDeadline(timeout) + tc.SetDeadline(timeout) if err := tc.SetKeepAlive(true); err != nil { // Don't send COM_QUIT before handshake. mc.netConn.Close()