Skip to content

Commit

Permalink
Adding a comment to explain reason for mutex locking and map copying.
Browse files Browse the repository at this point in the history
  • Loading branch information
fulghum committed May 31, 2022
1 parent 00c4184 commit 8ae7d39
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ func (s *SessionManager) SetDB(conn *mysql.Conn, db string) error {

// Iter iterates over the active sessions and executes the specified callback function on each one.
func (s *SessionManager) Iter(f func(session sql.Session) (stop bool, err error)) error {
// Lock the mutex guarding the sessions map while we make a copy of it to prevent errors from
// mutating a map while iterating over it. Making a copy of the map also allows us to guard
// against long running callback functions being passed in that could cause long mutex blocking.
s.mu.Lock()
sessionsCopy := make(map[uint32]*managedSession)
for key, value := range s.sessions {
Expand Down

0 comments on commit 8ae7d39

Please sign in to comment.