-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
60 lines (50 loc) · 1.27 KB
/
find.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
package dcs
import (
"sort"
"github.com/KoNekoD/td/tg"
)
// FindDCs searches DCs candidates from given config.
func FindDCs(opts []tg.DCOption, dcID int, preferIPv6 bool) []tg.DCOption {
// Preallocate slice.
candidates := make([]tg.DCOption, 0, 32)
for _, candidateDC := range opts {
if candidateDC.ID != dcID {
continue
}
candidates = append(candidates, candidateDC)
}
if len(candidates) < 1 {
return nil
}
sort.Slice(candidates, func(i, j int) bool {
l, r := candidates[i], candidates[j]
// If we prefer IPv6 and left is IPv6 and right is not, so then
// left is smaller (would be before right).
if preferIPv6 {
if l.Ipv6 && !r.Ipv6 {
return true
}
if !l.Ipv6 && r.Ipv6 {
return false
}
}
// Also we prefer static addresses.
return l.Static && !r.Static
})
return candidates
}
// FindPrimaryDCs searches new primary DC from given config.
// Unlike FindDC, it filters CDNs and MediaOnly servers, returns error
// if not found.
func FindPrimaryDCs(opts []tg.DCOption, dcID int, preferIPv6 bool) []tg.DCOption {
candidates := FindDCs(opts, dcID, preferIPv6)
// Filter (in place) from SliceTricks.
n := 0
for _, opt := range candidates {
if !opt.MediaOnly && !opt.CDN {
candidates[n] = opt
n++
}
}
return candidates[:n]
}