Skip to content

Commit

Permalink
move stale connection check to ResetSession()
Browse files Browse the repository at this point in the history
  • Loading branch information
methane committed Oct 9, 2023
1 parent 3798012 commit 8761c23
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
27 changes: 25 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type mysqlConn struct {
status statusFlag
sequence uint8
parseTime bool
reset bool // set when the Go SQL package calls ResetSession

// for context support (Go 1.8+)
watching bool
Expand Down Expand Up @@ -646,7 +645,31 @@ func (mc *mysqlConn) ResetSession(ctx context.Context) error {
if mc.closed.Load() {
return driver.ErrBadConn
}
mc.reset = true

// Perform a stale connection check. We only perform this check for
// the first query on a connection that has been checked out of the
// connection pool: a fresh connection from the pool is more likely
// to be stale, and it has not performed any previous writes that
// could cause data corruption, so it's safe to return ErrBadConn
// if the check fails.
if mc.cfg.CheckConnLiveness {
conn := mc.netConn
if mc.rawConn != nil {
conn = mc.rawConn
}
var err error
if mc.cfg.ReadTimeout != 0 {
err = conn.SetReadDeadline(time.Now().Add(mc.cfg.ReadTimeout))
}
if err == nil {
err = connCheck(conn)
}
if err != nil {
mc.cfg.Logger.Print("closing bad idle connection: ", err)
return driver.ErrBadConn
}
}

return nil
}

Expand Down
28 changes: 0 additions & 28 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,34 +98,6 @@ func (mc *mysqlConn) writePacket(data []byte) error {
return ErrPktTooLarge
}

// Perform a stale connection check. We only perform this check for
// the first query on a connection that has been checked out of the
// connection pool: a fresh connection from the pool is more likely
// to be stale, and it has not performed any previous writes that
// could cause data corruption, so it's safe to return ErrBadConn
// if the check fails.
if mc.reset {
mc.reset = false
conn := mc.netConn
if mc.rawConn != nil {
conn = mc.rawConn
}
var err error
if mc.cfg.CheckConnLiveness {
if mc.cfg.ReadTimeout != 0 {
err = conn.SetReadDeadline(time.Now().Add(mc.cfg.ReadTimeout))
}
if err == nil {
err = connCheck(conn)
}
}
if err != nil {
mc.cfg.Logger.Print("closing bad idle connection: ", err)
mc.Close()
return driver.ErrBadConn
}
}

for {
var size int
if pktLen >= maxPacketSize {
Expand Down

0 comments on commit 8761c23

Please sign in to comment.