This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
/
domain.go
144 lines (117 loc) · 2.86 KB
/
domain.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package tracker
import (
"sync"
"time"
)
type Domain struct {
name string
remotes *sync.Map
}
type DomainTimings struct {
Send time.Duration
Receive time.Duration
HasSend bool
HasReceive bool
}
var domainCache = &sync.Map{}
func GetDomain(name string) (*Domain) {
i, ok := domainCache.Load(name)
if !ok {
i = ResetDomain(name)
}
return i.(*Domain)
}
func ResetDomain(name string) (*Domain) {
i := &Domain{remotes: &sync.Map{}, name: name}
domainCache.Store(name, i)
return i
}
func (d *Domain) GetRemote(name string) (*RemoteDomain) {
i, ok := d.remotes.Load(name)
if !ok {
i = d.ResetRemote(name)
}
return i.(*RemoteDomain)
}
func (d *Domain) ResetRemote(name string) (*RemoteDomain) {
i := &RemoteDomain{rooms: &sync.Map{}, domain: d, name: name}
d.remotes.Store(name, i)
return i
}
func (d *Domain) GetRemotes() ([]string) {
r := make(map[string]interface{})
d.remotes.Range(func(k interface{}, v interface{}) bool {
r[k.(string)] = 1
return true // keep going if we can
})
remotes := make([]string, 0, len(r))
for k := range r {
remotes = append(remotes, k)
}
return remotes
}
func (d *Domain) CompareTo(other string) DomainTimings {
otherDomain := GetDomain(other)
remote := d.GetRemote(other)
usRemote := otherDomain.GetRemote(d.name)
// Calculate the send times (them -> us)
sendTimeTotal := int64(0)
sendTimeCount := 0
for _, roomId := range usRemote.GetRooms() {
room := usRemote.GetRoom(roomId)
for _, ping := range room.GetPings() {
sendTimeCount++
sendTimeTotal += ping.Record.ReceivedTs - ping.Record.GeneratedTs
}
}
// Calculate the receive times (us -> them)
receiveTimeTotal := int64(0)
receiveTimeCount := 0
for _, roomId := range remote.GetRooms() {
room := remote.GetRoom(roomId)
for _, ping := range room.GetPings() {
receiveTimeCount++
receiveTimeTotal += ping.Record.ReceivedTs - ping.Record.GeneratedTs
}
}
times := DomainTimings{}
if sendTimeCount > 0 {
times.HasSend = true
avg := float64(sendTimeTotal) / float64(sendTimeCount)
times.Send = time.Duration(avg) * time.Millisecond
} else {
times.HasSend = false
}
if receiveTimeCount > 0 {
times.HasReceive = true
avg := float64(receiveTimeTotal) / float64(receiveTimeCount)
times.Receive = time.Duration(avg) * time.Millisecond
} else {
times.HasReceive = false
}
return times
}
func GetDomains() []string {
return GetDomainsExcept() // No exceptions
}
func GetDomainsExcept(excludeDomains ...string) []string {
d := make(map[string]interface{})
domainCache.Range(func(k interface{}, v interface{}) bool {
d[k.(string)] = 1
return true // keep going if we can
})
domains := make([]string, 0, len(d))
for k := range d {
excluded := false
for _, e := range excludeDomains {
if e == k {
excluded = true
break
}
}
if !excluded {
domains = append(domains, k)
}
}
return domains
}