From e5b7c661d7fa9b42410e4eae837554d1753c88cb Mon Sep 17 00:00:00 2001 From: srfrog Date: Mon, 4 Feb 2019 20:05:24 -0700 Subject: [PATCH] switching to perfect use case of sync.Map and remove the locks --- dgraph/cmd/zero/zero.go | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/dgraph/cmd/zero/zero.go b/dgraph/cmd/zero/zero.go index d15fec1007e..a12fa2405a0 100644 --- a/dgraph/cmd/zero/zero.go +++ b/dgraph/cmd/zero/zero.go @@ -60,7 +60,7 @@ type Server struct { closer *y.Closer // Used to tell stream to close. connectLock sync.Mutex // Used to serialize connect requests from servers. - blockCommitsOn map[string]struct{} + blockCommitsOn *sync.Map } func (s *Server) Init() { @@ -78,7 +78,7 @@ func (s *Server) Init() { s.nextGroup = 1 s.leaderChangeCh = make(chan struct{}, 1) s.closer = y.NewCloser(2) // grpc and http - s.blockCommitsOn = make(map[string]struct{}) + s.blockCommitsOn = new(sync.Map) go s.rebalanceTablets() } @@ -285,20 +285,14 @@ func (s *Server) ServingTablet(tablet string) *pb.Tablet { } func (s *Server) blockTablet(pred string) func() { - s.Lock() - s.blockCommitsOn[pred] = struct{}{} - s.Unlock() + s.blockCommitsOn.Store(pred, struct{}{}) return func() { - s.Lock() - delete(s.blockCommitsOn, pred) - s.Unlock() + s.blockCommitsOn.Delete(pred) } } func (s *Server) isBlocked(pred string) bool { - s.RLock() - defer s.RUnlock() - _, blocked := s.blockCommitsOn[pred] + _, blocked := s.blockCommitsOn.Load(pred) return blocked }