add RWMutex for concurrent safe
This commit is contained in:
parent
e161d799da
commit
99dfe89f6b
14
hashring.go
14
hashring.go
@ -2,6 +2,7 @@ package hashring
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
|
"sync"
|
||||||
// "hash"
|
// "hash"
|
||||||
"math"
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
@ -30,6 +31,7 @@ type HashRing struct {
|
|||||||
virualSpots int
|
virualSpots int
|
||||||
nodes nodesArray
|
nodes nodesArray
|
||||||
weights map[string]int
|
weights map[string]int
|
||||||
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewHashRing create a hash ring with virual spots
|
//NewHashRing create a hash ring with virual spots
|
||||||
@ -41,12 +43,15 @@ func NewHashRing(spots int) *HashRing {
|
|||||||
h := &HashRing{
|
h := &HashRing{
|
||||||
virualSpots: spots,
|
virualSpots: spots,
|
||||||
weights: make(map[string]int),
|
weights: make(map[string]int),
|
||||||
|
mu: sync.RWMutex{},
|
||||||
}
|
}
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
//AddNodes add nodes to hash ring
|
//AddNodes add nodes to hash ring
|
||||||
func (h *HashRing) AddNodes(nodeWeight map[string]int) {
|
func (h *HashRing) AddNodes(nodeWeight map[string]int) {
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
for nodeKey, w := range nodeWeight {
|
for nodeKey, w := range nodeWeight {
|
||||||
h.weights[nodeKey] = w
|
h.weights[nodeKey] = w
|
||||||
}
|
}
|
||||||
@ -55,18 +60,24 @@ func (h *HashRing) AddNodes(nodeWeight map[string]int) {
|
|||||||
|
|
||||||
//AddNode add node to hash ring
|
//AddNode add node to hash ring
|
||||||
func (h *HashRing) AddNode(nodeKey string, weight int) {
|
func (h *HashRing) AddNode(nodeKey string, weight int) {
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
h.weights[nodeKey] = weight
|
h.weights[nodeKey] = weight
|
||||||
h.generate()
|
h.generate()
|
||||||
}
|
}
|
||||||
|
|
||||||
//RemoveNode remove node
|
//RemoveNode remove node
|
||||||
func (h *HashRing) RemoveNode(nodeKey string) {
|
func (h *HashRing) RemoveNode(nodeKey string) {
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
delete(h.weights, nodeKey)
|
delete(h.weights, nodeKey)
|
||||||
h.generate()
|
h.generate()
|
||||||
}
|
}
|
||||||
|
|
||||||
//UpdateNode update node with weight
|
//UpdateNode update node with weight
|
||||||
func (h *HashRing) UpdateNode(nodeKey string, weight int) {
|
func (h *HashRing) UpdateNode(nodeKey string, weight int) {
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
h.weights[nodeKey] = weight
|
h.weights[nodeKey] = weight
|
||||||
h.generate()
|
h.generate()
|
||||||
}
|
}
|
||||||
@ -107,6 +118,8 @@ func genValue(bs []byte) uint32 {
|
|||||||
|
|
||||||
//GetNode get node with key
|
//GetNode get node with key
|
||||||
func (h *HashRing) GetNode(s string) string {
|
func (h *HashRing) GetNode(s string) string {
|
||||||
|
h.mu.RLock()
|
||||||
|
defer h.mu.RUnlock()
|
||||||
if len(h.nodes) == 0 {
|
if len(h.nodes) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -120,6 +133,5 @@ func (h *HashRing) GetNode(s string) string {
|
|||||||
if i == len(h.nodes) {
|
if i == len(h.nodes) {
|
||||||
i = 0
|
i = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
return h.nodes[i].nodeKey
|
return h.nodes[i].nodeKey
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user