From a46b053aa626a005a30dfb1ac4e096abcce1ef76 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Mon, 22 Apr 2019 12:50:13 +0300 Subject: [PATCH] More granular locking --- ring.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ring.go b/ring.go index 1fd0a72..a582aef 100644 --- a/ring.go +++ b/ring.go @@ -273,16 +273,20 @@ func (c *ringShards) Heartbeat(frequency time.Duration) { // rebalance removes dead shards from the Ring. func (c *ringShards) rebalance() { + c.mu.RLock() + shards := c.shards + c.mu.RUnlock() + hash := newConsistentHash(c.opt) var shardsNum int - c.mu.Lock() - for name, shard := range c.shards { + for name, shard := range shards { if shard.IsUp() { hash.Add(name) shardsNum++ } } + c.mu.Lock() c.hash = hash c.len = shardsNum c.mu.Unlock()