/
consistent_hash.go
67 lines (55 loc) · 1.33 KB
/
consistent_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
package dnsregistry
// Taken from https://github.com/golang/groupcache/blob/master/consistenthash/consistenthash.go
// Apache licensed.
import (
"hash/crc32"
"sort"
"strconv"
)
type Hash func(data []byte) uint32
type HashRing struct {
hash Hash
replicas int
keys []int // Sorted
hashMap map[int]string
}
func NewHashRing(replicas int, fn Hash) *HashRing {
m := &HashRing{
replicas: replicas,
hash: fn,
hashMap: make(map[int]string),
}
if m.hash == nil {
m.hash = crc32.ChecksumIEEE
}
return m
}
// IsEmpty returns true if there are no items available.
func (m *HashRing) IsEmpty() bool {
return len(m.keys) == 0
}
// Add adds some keys to the hash.
func (m *HashRing) Add(keys ...string) {
for _, key := range keys {
for i := 0; i < m.replicas; i++ {
hash := int(m.hash([]byte(strconv.Itoa(i) + key)))
m.keys = append(m.keys, hash)
m.hashMap[hash] = key
}
}
sort.Ints(m.keys)
}
// Get gets the closest item in the hash to the provided key.
func (m *HashRing) Get(key string) string {
if m.IsEmpty() {
return ""
}
hash := int(m.hash([]byte(key)))
// Binary search for appropriate replica.
idx := sort.Search(len(m.keys), func(i int) bool { return m.keys[i] >= hash })
// Means we have cycled back to the first replica.
if idx == len(m.keys) {
idx = 0
}
return m.hashMap[m.keys[idx]]
}