Skip to content

Commit 5f74bcb

Browse files
authored
move stale connection check to ResetSession() (#1496)
When ResetSession was added, it was called when the connection is put into the pool. Thet is why we had only set `mc.reset` flag on ResetSession(). In Go 1.15, this behavior was changed. (golang/go@971f8a2) ResetSession is called when the connection is checked out from the pool. So we can call checkConnLiveness() directly from ResetSession.
1 parent 3798012 commit 5f74bcb

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

Diff for: connection.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ type mysqlConn struct {
3434
status statusFlag
3535
sequence uint8
3636
parseTime bool
37-
reset bool // set when the Go SQL package calls ResetSession
3837

3938
// for context support (Go 1.8+)
4039
watching bool
@@ -646,7 +645,31 @@ func (mc *mysqlConn) ResetSession(ctx context.Context) error {
646645
if mc.closed.Load() {
647646
return driver.ErrBadConn
648647
}
649-
mc.reset = true
648+
649+
// Perform a stale connection check. We only perform this check for
650+
// the first query on a connection that has been checked out of the
651+
// connection pool: a fresh connection from the pool is more likely
652+
// to be stale, and it has not performed any previous writes that
653+
// could cause data corruption, so it's safe to return ErrBadConn
654+
// if the check fails.
655+
if mc.cfg.CheckConnLiveness {
656+
conn := mc.netConn
657+
if mc.rawConn != nil {
658+
conn = mc.rawConn
659+
}
660+
var err error
661+
if mc.cfg.ReadTimeout != 0 {
662+
err = conn.SetReadDeadline(time.Now().Add(mc.cfg.ReadTimeout))
663+
}
664+
if err == nil {
665+
err = connCheck(conn)
666+
}
667+
if err != nil {
668+
mc.cfg.Logger.Print("closing bad idle connection: ", err)
669+
return driver.ErrBadConn
670+
}
671+
}
672+
650673
return nil
651674
}
652675

Diff for: packets.go

-28
Original file line numberDiff line numberDiff line change
@@ -98,34 +98,6 @@ func (mc *mysqlConn) writePacket(data []byte) error {
9898
return ErrPktTooLarge
9999
}
100100

101-
// Perform a stale connection check. We only perform this check for
102-
// the first query on a connection that has been checked out of the
103-
// connection pool: a fresh connection from the pool is more likely
104-
// to be stale, and it has not performed any previous writes that
105-
// could cause data corruption, so it's safe to return ErrBadConn
106-
// if the check fails.
107-
if mc.reset {
108-
mc.reset = false
109-
conn := mc.netConn
110-
if mc.rawConn != nil {
111-
conn = mc.rawConn
112-
}
113-
var err error
114-
if mc.cfg.CheckConnLiveness {
115-
if mc.cfg.ReadTimeout != 0 {
116-
err = conn.SetReadDeadline(time.Now().Add(mc.cfg.ReadTimeout))
117-
}
118-
if err == nil {
119-
err = connCheck(conn)
120-
}
121-
}
122-
if err != nil {
123-
mc.cfg.Logger.Print("closing bad idle connection: ", err)
124-
mc.Close()
125-
return driver.ErrBadConn
126-
}
127-
}
128-
129101
for {
130102
var size int
131103
if pktLen >= maxPacketSize {

0 commit comments

Comments
 (0)