Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix spurious errors when closing inactive connections #408

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously when this was in ::getSession, it would create the session, which would trigger the spurious error if the Conn had no attributes because it wasn't properly established in the first place.

}

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
Loading