From 8f512145337eb41a2f4e68eb23f12690a1029065 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 31 Jul 2019 17:24:44 +0800 Subject: [PATCH] server: move log operation out of lock scope (#11536) 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 --- server/server.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/server/server.go b/server/server.go index 6d1945fb6b22a..aca5125bef656 100644 --- a/server/server.go +++ b/server/server.go @@ -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 @@ -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) } }