More granular locking

This commit is contained in:
Vladimir Mihailenco 2019-04-22 12:50:13 +03:00
parent be4c4f3f38
commit a46b053aa6
1 changed files with 6 additions and 2 deletions

View File

@ -273,16 +273,20 @@ func (c *ringShards) Heartbeat(frequency time.Duration) {
// rebalance removes dead shards from the Ring. // rebalance removes dead shards from the Ring.
func (c *ringShards) rebalance() { func (c *ringShards) rebalance() {
c.mu.RLock()
shards := c.shards
c.mu.RUnlock()
hash := newConsistentHash(c.opt) hash := newConsistentHash(c.opt)
var shardsNum int var shardsNum int
c.mu.Lock() for name, shard := range shards {
for name, shard := range c.shards {
if shard.IsUp() { if shard.IsUp() {
hash.Add(name) hash.Add(name)
shardsNum++ shardsNum++
} }
} }
c.mu.Lock()
c.hash = hash c.hash = hash
c.len = shardsNum c.len = shardsNum
c.mu.Unlock() c.mu.Unlock()