Skip to content

Commit

Permalink
server: move log operation out of lock scope (#11536)
Browse files Browse the repository at this point in the history
It's a bad style to print log under a lock, tiny refactor to make the code better.
Lock() is also changed to RLock() to reduce lock contend
  • Loading branch information
tiancaiamao authored and ngaut committed Jul 31, 2019
1 parent 9f68166 commit 8f51214
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,11 @@ func (s *Server) GetProcessInfo(id uint64) (*util.ProcessInfo, bool) {

// Kill implements the SessionManager interface.
func (s *Server) Kill(connectionID uint64, query bool) {
s.rwlock.Lock()
defer s.rwlock.Unlock()
logutil.BgLogger().Info("kill", zap.Uint64("connID", connectionID), zap.Bool("query", query))
metrics.ServerEventCounter.WithLabelValues(metrics.EventKill).Inc()

s.rwlock.RLock()
defer s.rwlock.RUnlock()
conn, ok := s.clients[uint32(connectionID)]
if !ok {
return
Expand All @@ -538,13 +538,15 @@ func killConn(conn *clientConn) {

// KillAllConnections kills all connections when server is not gracefully shutdown.
func (s *Server) KillAllConnections() {
s.rwlock.Lock()
defer s.rwlock.Unlock()
logutil.BgLogger().Info("[server] kill all connections.")

s.rwlock.RLock()
defer s.rwlock.RUnlock()
for _, conn := range s.clients {
atomic.StoreInt32(&conn.status, connStatusShutdown)
terror.Log(errors.Trace(conn.closeWithoutLock()))
if err := conn.closeWithoutLock(); err != nil {
terror.Log(err)
}
killConn(conn)
}
}
Expand Down

0 comments on commit 8f51214

Please sign in to comment.