Skip to content

Commit

Permalink
server/context.go: Add a method on SessionManager which allows an int…
Browse files Browse the repository at this point in the history
…egrator to wait for all client connections to drain.
  • Loading branch information
reltuk committed Mar 5, 2025
1 parent 690400e commit 31f800f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions server/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type SessionManager struct {
connections map[uint32]*mysql.Conn
lastPid uint64
ctxFactory sql.ContextFactory
// Implements WaitForClosedConnections(), which is only used
// at server shutdown to allow the integrator to ensure that
// no connections are being handled by handlers.
wg sync.WaitGroup
}

// NewSessionManager creates a SessionManager with the given SessionBuilder.
Expand Down Expand Up @@ -82,6 +86,13 @@ func (s *SessionManager) nextPid() uint64 {
return s.lastPid
}

// Block the calling thread until all known connections are closed. It
// is an error to call this concurrently while the server might still
// be accepting new connections.
func (s *SessionManager) WaitForClosedConnections() {
s.wg.Wait()
}

// AddConn adds a connection to be tracked by the SessionManager. Should be called as
// soon as possible after the server has accepted the connection. Results in
// the connection being tracked by ProcessList and being available through
Expand All @@ -93,6 +104,7 @@ func (s *SessionManager) AddConn(conn *mysql.Conn) {
defer s.mu.Unlock()
s.connections[conn.ConnectionID] = conn
s.processlist.AddConnection(conn.ConnectionID, conn.RemoteAddr().String())
s.wg.Add(1)
}

// NewSession creates a Session for the given connection and saves it to the session pool.
Expand Down Expand Up @@ -270,6 +282,7 @@ func (s *SessionManager) KillConnection(connID uint32) error {
func (s *SessionManager) RemoveConn(conn *mysql.Conn) {
s.mu.Lock()
defer s.mu.Unlock()
s.wg.Done()
if cur, ok := s.sessions[conn.ConnectionID]; ok {
sql.SessionEnd(cur)
}
Expand Down

0 comments on commit 31f800f

Please sign in to comment.