Skip to content

Commit 5351727

Browse files
PR feedback: log error if setting tcp keep-alive failed
Signed-off-by: Achille Roussel <[email protected]>
1 parent 4eb009c commit 5351727

File tree

1 file changed

+4
-22
lines changed

1 file changed

+4
-22
lines changed

connector.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ package mysql
1111
import (
1212
"context"
1313
"database/sql/driver"
14-
"errors"
1514
"fmt"
1615
"net"
1716
"os"
1817
"strconv"
1918
"strings"
20-
"syscall"
2119
)
2220

2321
type connector struct {
@@ -100,11 +98,10 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
10098
}
10199

102100
// Enable TCP Keepalives on TCP connections
103-
if err := enableKeepAlive(mc.netConn); err != nil {
104-
// Don't send COM_QUIT before handshake.
105-
mc.netConn.Close()
106-
mc.netConn = nil
107-
return nil, err
101+
if tc, ok := mc.netConn.(*net.TCPConn); ok {
102+
if err := tc.SetKeepAlive(true); err != nil {
103+
c.cfg.Logger.Print(err)
104+
}
108105
}
109106

110107
// Call startWatcher for context support (From Go 1.8)
@@ -188,18 +185,3 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
188185
func (c *connector) Driver() driver.Driver {
189186
return &MySQLDriver{}
190187
}
191-
192-
func enableKeepAlive(nc net.Conn) error {
193-
if tc, ok := nc.(*net.TCPConn); ok {
194-
if err := tc.SetKeepAlive(true); err != nil {
195-
// The underlying setsockopt syscall may return ENOPROTOOPT if the
196-
// system does not support TCP keep-alive. We can still successfully
197-
// use the driver without keep-alive support, which is why we choose
198-
// to silence it here.
199-
if !errors.Is(err, syscall.ENOPROTOOPT) {
200-
return err
201-
}
202-
}
203-
}
204-
return nil
205-
}

0 commit comments

Comments
 (0)