Skip to content

Commit

Permalink
Merge pull request #16 from konradreiche/ping-data-race
Browse files Browse the repository at this point in the history
Synchronize read & write access to node ping to fix data race
  • Loading branch information
akostrikov authored Apr 26, 2021
2 parents ea26ef8 + b09be50 commit 1283dc7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion rediscluster/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ func (n *node) updatePingLatency() {
latency = l
}
}
n.ping = uint32(latency / redisconn.PingLatencyGranularity)
ping := uint32(latency / redisconn.PingLatencyGranularity)
atomic.StoreUint32(&n.ping, ping)
}

func nextRng(state *uint32, mod uint32) uint32 {
Expand Down
4 changes: 2 additions & 2 deletions rediscluster/slotrange.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ func (c *Cluster) updateMappings(slotRanges []redisclusterutil.SlotsRange) {
sumLatency := uint32(0)
for _, addr := range shard.addr {
node := newConfig.nodes[addr]
sumLatency += node.ping
sumLatency += atomic.LoadUint32(&node.ping)
}
for i, addr := range shard.addr {
node := newConfig.nodes[addr]
weight := sumLatency / node.ping
weight := sumLatency / atomic.LoadUint32(&node.ping)
atomic.StoreUint32(&shard.weights[i], weight)
}
}
Expand Down

0 comments on commit 1283dc7

Please sign in to comment.