/
sorters.go
45 lines (37 loc) · 1.43 KB
/
sorters.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
package http2
import (
"crypto/md5"
"encoding/binary"
)
type instancePoolSlice []*instancePool
func (s instancePoolSlice) Len() int { return len(s) }
func (s instancePoolSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// instancePoolSlice sorts by instanceId in descending order.
func (s instancePoolSlice) Less(i, j int) bool { return s[i].instanceId > s[j].instanceId }
type shuffleSortHelper struct {
shuffleSeed int
instances []*instancePool
}
func (s shuffleSortHelper) sortIdx(idx int) uint64 {
var buffer [16]byte
binary.LittleEndian.PutUint64(buffer[:8], uint64(s.shuffleSeed))
binary.LittleEndian.PutUint64(buffer[8:], uint64(s.instances[idx].instanceId))
sum := md5.Sum(buffer[:])
return binary.LittleEndian.Uint64(sum[:8])
}
func (s shuffleSortHelper) Len() int { return len(s.instances) }
func (s shuffleSortHelper) Swap(i, j int) {
s.instances[i], s.instances[j] = s.instances[j], s.instances[i]
}
func (s shuffleSortHelper) Less(i, j int) bool { return s.sortIdx(i) < s.sortIdx(j) }
// Consistent hashing
type consistentHashSortHelper struct {
Instances []*instancePool
Hashes []uint32
}
func (s consistentHashSortHelper) Len() int { return len(s.Instances) }
func (s consistentHashSortHelper) Swap(i, j int) {
s.Instances[i], s.Instances[j] = s.Instances[j], s.Instances[i]
s.Hashes[i], s.Hashes[j] = s.Hashes[j], s.Hashes[i]
}
func (s consistentHashSortHelper) Less(i, j int) bool { return s.Hashes[i] < s.Hashes[j] }