-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.go
85 lines (78 loc) · 2.25 KB
/
hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package goloadbalancer
import (
"fmt"
"hash/crc32"
"sort"
"sync"
)
// HashCircle 一致性哈希环
type HashCircle map[uint32]Endpoint
type ConsistentHashBalancer struct {
circle HashCircle
sortedKeys []uint32
vNodes int // 每个实际节点对应的虚拟节点数量
hashFn func(data []byte) uint32 // 哈希函数
*BaseLoadBalance
}
func NewConsistentHashBalancer(endpoints []Endpoint, vNodes int) LoadBalance {
b := &ConsistentHashBalancer{
circle: make(HashCircle),
sortedKeys: []uint32{},
vNodes: vNodes,
hashFn: crc32.ChecksumIEEE,
BaseLoadBalance: &BaseLoadBalance{
endpoints: endpoints,
lock: sync.RWMutex{},
},
}
for _, endpoint := range endpoints {
b.AddEndpoint(endpoint)
}
return b
}
func (c *ConsistentHashBalancer) AddEndpoint(ep interface{}) {
c.lock.Lock()
defer c.lock.Unlock()
endpoint := ep.(Endpoint)
// 每个实际节点对应vNodes个虚拟节点
for i := 0; i < c.vNodes; i++ {
vNodeKey := fmt.Sprintf("%s#%d", endpoint.Addr(), i)
hash := c.hashFn([]byte(vNodeKey))
c.circle[hash] = endpoint
c.sortedKeys = append(c.sortedKeys, hash)
}
sort.Slice(c.sortedKeys, func(i, j int) bool { return c.sortedKeys[i] < c.sortedKeys[j] })
}
func (c *ConsistentHashBalancer) Select(args ...interface{}) (Endpoint, error) {
c.lock.RLock()
defer c.lock.RUnlock()
if len(c.endpoints) == 0 {
return nil, ErrNoEndpoint
}
if len(args) == 0 {
return nil, ErrNoSourceIP
}
sourceIP := args[0].(string)
hash := c.hashFn([]byte(sourceIP))
idx := sort.Search(len(c.sortedKeys), func(i int) bool { return c.sortedKeys[i] >= hash })
if idx == len(c.sortedKeys) {
idx = 0
}
return c.circle[c.sortedKeys[idx]], nil
}
func (c *ConsistentHashBalancer) RemoveEndpoint(endpoint Endpoint) {
c.lock.Lock()
defer c.lock.Unlock()
for i := 0; i < c.vNodes; i++ {
vNodeKey := fmt.Sprintf("%s#%d", endpoint.Addr(), i)
hash := c.hashFn([]byte(vNodeKey))
if _, exists := c.circle[hash]; exists {
delete(c.circle, hash)
index := sort.Search(len(c.sortedKeys), func(i int) bool { return c.sortedKeys[i] == hash })
c.sortedKeys = append(c.sortedKeys[:index], c.sortedKeys[index+1:]...)
}
}
}
func (c *ConsistentHashBalancer) Name() string {
return string(ConsistentHashBalanceType)
}