-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
36 lines (33 loc) · 960 Bytes
/
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
package balancer
//Hash jump to a node using consistent hash
type Hash struct {
}
//SelectNode select a node based on hash
func (h *Hash) SelectNode(balancer *Balancer, clientID string) Node {
poolSize := uint32(len(balancer.UpstreamPool))
if poolSize == 0 {
return nil
}
index := findIndex(clientID, poolSize)
if upstream := balancer.UpstreamPool[index]; upstream.IsHealthy() {
return upstream
}
for i := uint32(0); i < poolSize; i++ {
if upstream := balancer.UpstreamPool[i]; upstream.IsHealthy() {
return upstream
}
}
return nil
}
// findIndex finds consistent index using golang fast hash
// https://github.com/golang/go/blob/master/src/hash/fnv/fnv.go#L100 the code we previously used
// has some allocations
// we need minimum footprint per deciding
func findIndex(s string, poolSize uint32) uint32 {
var h32a uint32 = 2166136261
for _, c := range []byte(s) {
h32a *= 16777619
h32a ^= uint32(c)
}
return h32a % poolSize
}