From 4eb009c9f26068e1499d1c5af041d13aff86cdd1 Mon Sep 17 00:00:00 2001 From: Achille Roussel Date: Sun, 11 Jun 2023 17:09:08 -0700 Subject: [PATCH 1/2] tcp: ignore ENOPROTOOPT returned by SetKeepAlive on some platforms Signed-off-by: Achille Roussel --- connector.go | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/connector.go b/connector.go index 6acf3dd50..c83ba66c0 100644 --- a/connector.go +++ b/connector.go @@ -11,11 +11,13 @@ package mysql import ( "context" "database/sql/driver" + "errors" "fmt" "net" "os" "strconv" "strings" + "syscall" ) type connector struct { @@ -98,13 +100,11 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { } // Enable TCP Keepalives on TCP connections - if tc, ok := mc.netConn.(*net.TCPConn); ok { - if err := tc.SetKeepAlive(true); err != nil { - // Don't send COM_QUIT before handshake. - mc.netConn.Close() - mc.netConn = nil - return nil, err - } + if err := enableKeepAlive(mc.netConn); err != nil { + // Don't send COM_QUIT before handshake. + mc.netConn.Close() + mc.netConn = nil + return nil, err } // Call startWatcher for context support (From Go 1.8) @@ -188,3 +188,18 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { func (c *connector) Driver() driver.Driver { return &MySQLDriver{} } + +func enableKeepAlive(nc net.Conn) error { + if tc, ok := nc.(*net.TCPConn); ok { + if err := tc.SetKeepAlive(true); err != nil { + // The underlying setsockopt syscall may return ENOPROTOOPT if the + // system does not support TCP keep-alive. We can still successfully + // use the driver without keep-alive support, which is why we choose + // to silence it here. + if !errors.Is(err, syscall.ENOPROTOOPT) { + return err + } + } + } + return nil +} From 53517277a61cbe07496d760fa93094c7d34a835c Mon Sep 17 00:00:00 2001 From: Achille Roussel Date: Mon, 12 Jun 2023 10:21:37 -0700 Subject: [PATCH 2/2] PR feedback: log error if setting tcp keep-alive failed Signed-off-by: Achille Roussel --- connector.go | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/connector.go b/connector.go index c83ba66c0..7e0b16734 100644 --- a/connector.go +++ b/connector.go @@ -11,13 +11,11 @@ package mysql import ( "context" "database/sql/driver" - "errors" "fmt" "net" "os" "strconv" "strings" - "syscall" ) type connector struct { @@ -100,11 +98,10 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { } // Enable TCP Keepalives on TCP connections - if err := enableKeepAlive(mc.netConn); err != nil { - // Don't send COM_QUIT before handshake. - mc.netConn.Close() - mc.netConn = nil - return nil, err + if tc, ok := mc.netConn.(*net.TCPConn); ok { + if err := tc.SetKeepAlive(true); err != nil { + c.cfg.Logger.Print(err) + } } // Call startWatcher for context support (From Go 1.8) @@ -188,18 +185,3 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { func (c *connector) Driver() driver.Driver { return &MySQLDriver{} } - -func enableKeepAlive(nc net.Conn) error { - if tc, ok := nc.(*net.TCPConn); ok { - if err := tc.SetKeepAlive(true); err != nil { - // The underlying setsockopt syscall may return ENOPROTOOPT if the - // system does not support TCP keep-alive. We can still successfully - // use the driver without keep-alive support, which is why we choose - // to silence it here. - if !errors.Is(err, syscall.ENOPROTOOPT) { - return err - } - } - } - return nil -}