File tree Expand file tree Collapse file tree 1 file changed +22
-7
lines changed Expand file tree Collapse file tree 1 file changed +22
-7
lines changed Original file line number Diff line number Diff line change @@ -11,11 +11,13 @@ package mysql
11
11
import (
12
12
"context"
13
13
"database/sql/driver"
14
+ "errors"
14
15
"fmt"
15
16
"net"
16
17
"os"
17
18
"strconv"
18
19
"strings"
20
+ "syscall"
19
21
)
20
22
21
23
type connector struct {
@@ -98,13 +100,11 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
98
100
}
99
101
100
102
// Enable TCP Keepalives on TCP connections
101
- if tc , ok := mc .netConn .(* net.TCPConn ); ok {
102
- if err := tc .SetKeepAlive (true ); err != nil {
103
- // Don't send COM_QUIT before handshake.
104
- mc .netConn .Close ()
105
- mc .netConn = nil
106
- return nil , err
107
- }
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
108
108
}
109
109
110
110
// Call startWatcher for context support (From Go 1.8)
@@ -188,3 +188,18 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
188
188
func (c * connector ) Driver () driver.Driver {
189
189
return & MySQLDriver {}
190
190
}
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
+ }
You can’t perform that action at this time.
0 commit comments