Skip to content

Commit

Permalink
PR feedback: log error if setting tcp keep-alive failed
Browse files Browse the repository at this point in the history
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
  • Loading branch information
achille-roussel committed Jun 12, 2023
1 parent 4eb009c commit 5351727
Showing 1 changed file with 4 additions and 22 deletions.
26 changes: 4 additions & 22 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ package mysql
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"net"
"os"
"strconv"
"strings"
"syscall"
)

type connector struct {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit 5351727

Please sign in to comment.