From 969cee4e7c10a4dce932b0d1624af3f78b40aa81 Mon Sep 17 00:00:00 2001 From: Rodrigo Brito Date: Mon, 28 Jun 2021 13:42:12 -0300 Subject: [PATCH] refactor(health): replace mutex with atomic --- server.go | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/server.go b/server.go index b2e30d0..1ca58de 100644 --- a/server.go +++ b/server.go @@ -6,6 +6,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "github.com/go-gorp/gorp" ) @@ -14,14 +15,13 @@ var mutex sync.Mutex // Server server representation type Server struct { - sync.RWMutex name string health *ServerHealth serverSettings ServerSettings connection *gorp.DbMap replicationConnection *gorp.DbMap traceOn bool - isChecking bool + isChecking int32 } // GetName returns server's name @@ -71,22 +71,13 @@ func (s *Server) CheckHealth(traceOn bool, logger Logger) { var secondsBehindMaster, openConnections, runningConnections *int // prevent concurrently checks on same server (slow queries/network) - s.RLock() - checking := s.isChecking - s.RUnlock() - - if checking { + if atomic.LoadInt32(&s.isChecking) == 1 { return } - s.Lock() - s.isChecking = true - s.Unlock() - + atomic.StoreInt32(&s.isChecking, 1) defer func() { - s.Lock() - s.isChecking = false - s.Unlock() + atomic.StoreInt32(&s.isChecking, 0) }() if err := s.connectReadUser(traceOn, logger); err != nil {