-
Notifications
You must be signed in to change notification settings - Fork 193
/
picker.go
63 lines (48 loc) · 1.14 KB
/
picker.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
package main
import (
"github.com/miekg/dns"
"math/rand"
)
func (label *Label) Picker(qtype uint16, max int) Records {
if qtype == dns.TypeANY {
result := make([]Record, 0)
for rtype, _ := range label.Records {
rtype_records := label.Picker(rtype, max)
tmp_result := make(Records, len(result)+len(rtype_records))
copy(tmp_result, result)
copy(tmp_result[len(result):], rtype_records)
result = tmp_result
}
return result
}
if label_rr := label.Records[qtype]; label_rr != nil {
// not "balanced", just return all
if label.Weight[qtype] == 0 {
return label_rr
}
rr_count := len(label_rr)
if max > rr_count {
max = rr_count
}
servers := make([]Record, len(label_rr))
copy(servers, label_rr)
result := make([]Record, max)
sum := label.Weight[qtype]
for si := 0; si < max; si++ {
n := rand.Intn(sum + 1)
s := 0
for i := range servers {
s += int(servers[i].Weight)
if s >= n {
sum -= servers[i].Weight
result[si] = servers[i]
// remove the server from the list
servers = append(servers[:i], servers[i+1:]...)
break
}
}
}
return result
}
return nil
}