-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdomain.go
176 lines (141 loc) · 2.85 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package lrmp
import (
"container/list"
"time"
)
const (
MaxDisableTime = 180000 /* millis */
DisableTries = 5
MinRTTValue = 2
MaxRTTValue = 16000
)
type lossHistory struct {
list.List
}
func (lh *lossHistory) contains(ev *lossEvent) bool {
for next := lh.Front(); next != nil; next = next.Next() {
if next.Value == ev {
return true
}
}
return false
}
const historySize = 16
type domain struct {
lastTimeToggle time.Time
failedNack int
lossTab *lossTable
child *domain
lossHistory *lossHistory
parent *domain
stats DomainStats
scope int
initialMRTT int
}
func (d *domain) updateMRTT(rtt int) {
}
func (d *domain) setChild(child *domain) {
d.child = child
d.child.parent = d
d.stats.childScope = child.scope
}
func (d *domain) checkState() {
/* top level domain always enabled */
if d.parent == nil {
return
}
if d.stats.enabled {
if d.failedNack > DisableTries {
d.disable()
}
} else {
if time.Now().Sub(d.lastTimeToggle) > time.Duration(time.Millisecond*MaxDisableTime) {
d.enable()
}
}
}
func (d *domain) enable() {
if d.stats.enabled {
return
}
d.stats.enabled = true
d.failedNack = 0
d.lastTimeToggle = time.Now()
if d.parent != nil {
d.parent.enable()
}
if isDebug() {
logDebug("ENABLE scope=", d.scope)
}
}
func (d *domain) disable() {
if d.parent == nil || !d.stats.enabled {
return
}
d.stats.enabled = false
d.lastTimeToggle = time.Now()
if d.child != nil {
d.child.disable()
}
if isDebug() {
logDebug("DISABLE ", d.scope, " fails=", d.failedNack)
}
}
func (d *domain) isEnabled() bool {
return d.stats.enabled
}
func (d *domain) isDuplicate(event *lossEvent) bool {
dup := false
slice := d.stats.mrtt >> 3
if event.source.interval < 200 {
slice += event.source.interval
} else {
slice += 200
}
for elem := d.lossHistory.Back(); elem != nil; elem = elem.Prev() {
ev1 := elem.Value.(*lossEvent)
if ev1.source != event.source {
continue
}
diff := int(millis(event.rcvSendTime.Sub(ev1.rcvSendTime)))
if diff < slice {
if ev1.contains(event) {
dup = true
if isDebug() {
logDebug("Dup NACK: ", diff, "<", slice)
}
break
}
} else {
/* keep the most recent */
if event.contains(ev1) {
if isDebug() {
logDebug("Repeated nack in ", diff)
}
d.lossHistory.Remove(elem)
}
}
}
if d.lossHistory.Len() > historySize {
d.lossHistory.Remove(d.lossHistory.Front())
}
d.lossHistory.PushBack(event)
return dup
}
func newDomain(ttl int) *domain {
d := domain{scope: ttl}
d.stats.enabled = true
d.stats.childScope = 0
/* 200*(scope/63)^2 */
d.initialMRTT = getInitialRTT(d.scope)
d.stats.mrtt = d.initialMRTT << 3
return &d
}
func getInitialRTT(ttl int) int {
if ttl <= 15 {
return 12
} else if ttl >= 126 {
return 800
}
return (200*ttl*ttl + 1984) / 3969
}