Skip to content

Commit

Permalink
fix spurious errors when closing inactive connections (#408)
Browse files Browse the repository at this point in the history
When a connection is closed or reset before it is properly established, the
existing ::getSession will log a spurious error since the connection has no
attributes to extract for the pool type.

Fix this by refactoring so that when closing a connection we only act on the
session object if one already existed, i.e. the conn was used before.
  • Loading branch information
demmer committed Jun 11, 2024
1 parent d328dd4 commit 8f114fa
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions go/vt/vtgateproxy/mysql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,7 @@ func (ph *proxyHandler) NewConnection(c *mysql.Conn) {

func (ph *proxyHandler) ComResetConnection(c *mysql.Conn) {
ctx := context.Background()
session, err := ph.getSession(ctx, c)
if err != nil {
return
}
if session.SessionPb().InTransaction {
defer atomic.AddInt32(&busyConnections, -1)
}
err = ph.proxy.CloseSession(ctx, session)
if err != nil {
log.Errorf("Error happened in transaction rollback: %v", err)
}
ph.closeSession(ctx, c)
}

func (ph *proxyHandler) ConnectionClosed(c *mysql.Conn) {
Expand All @@ -127,14 +117,7 @@ func (ph *proxyHandler) ConnectionClosed(c *mysql.Conn) {
} else {
ctx = context.Background()
}
session, err := ph.getSession(ctx, c)
if err != nil {
return
}
if session.SessionPb().InTransaction {
defer atomic.AddInt32(&busyConnections, -1)
}
_ = ph.proxy.CloseSession(ctx, session)
ph.closeSession(ctx, c)
}

// Regexp to extract parent span id over the sql query
Expand Down Expand Up @@ -377,6 +360,23 @@ func (ph *proxyHandler) getSession(ctx context.Context, c *mysql.Conn) (*vtgatec
return session, nil
}

func (ph *proxyHandler) closeSession(ctx context.Context, c *mysql.Conn) {
session, _ := c.ClientData.(*vtgateconn.VTGateSession)
if session == nil {
return // no active session
}

if session.SessionPb().InTransaction {
defer atomic.AddInt32(&busyConnections, -1)
}
err := ph.proxy.CloseSession(ctx, session)
if err != nil {
log.Errorf("Error happened in transaction rollback: %v", err)
}

c.ClientData = nil
}

var mysqlListener *mysql.Listener
var mysqlUnixListener *mysql.Listener
var sigChan chan os.Signal
Expand Down

0 comments on commit 8f114fa

Please sign in to comment.