-
Notifications
You must be signed in to change notification settings - Fork 0
/
congestion.go
155 lines (124 loc) · 2.67 KB
/
congestion.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
package kcp
import (
"sync"
)
const (
defaultRTT = 100
queueSize = 10
)
type Queue struct {
value [queueSize]uint32
start uint32
length uint32
}
func (v *Queue) Push(value uint32) {
if v.length < queueSize {
v.value[v.length] = value
v.length++
return
}
v.value[v.start] = value
v.start++
if v.start == queueSize {
v.start = 0
}
}
func (v *Queue) Max() uint32 {
max := v.value[0]
for i := 1; i < queueSize; i++ {
if v.value[i] > max {
max = v.value[i]
}
}
return max
}
func (v *Queue) Min() uint32 {
max := v.value[0]
for i := 1; i < queueSize; i++ {
if v.value[i] < max {
max = v.value[i]
}
}
return max
}
type CongestionState byte
const (
CongestionStateRTTProbe CongestionState = iota
CongestionStateBandwidthProbe
CongestionStateTransfer
)
type Congestion struct {
sync.RWMutex
state CongestionState
stateSince uint32
limit uint32 // bytes per 1000 seconds
rtt uint32 // millisec
rttHistory Queue
rttUpdateTime uint32
initialThroughput uint32 // bytes per 1000 seconds
cycleStartTime uint32
cycleBytesConfirmed uint32
cycleBytesSent uint32
cycleBytesLimit uint32
cycle uint32
bestCycleBytesConfirmed uint32
bestCycleBytesSent uint32
}
func (v *Congestion) SetState(current uint32, state CongestionState) {
v.state = state
v.stateSince = current
}
func (v *Congestion) Update(current uint32) {
switch v.state {
case CongestionStateRTTProbe:
if v.rtt > 0 {
v.SetState(current, CongestionStateBandwidthProbe)
v.cycleStartTime = current
v.cycleBytesConfirmed = 0
v.cycleBytesSent = 0
v.cycleBytesLimit = v.initialThroughput * v.rtt / 1000 / 1000
}
case CongestionStateBandwidthProbe:
if current-v.cycleStartTime >= v.rtt {
}
}
}
func (v *Congestion) AddBytesConfirmed(current uint32, bytesConfirmed uint32) {
v.Lock()
defer v.Unlock()
v.cycleBytesConfirmed += bytesConfirmed
v.Update(current)
}
func (v *Congestion) UpdateRTT(current uint32, rtt uint32) {
v.Lock()
defer v.Unlock()
if v.state == CongestionStateRTTProbe || rtt < v.rtt {
v.rtt = rtt
v.rttUpdateTime = current
}
v.Update(current)
}
func (v *Congestion) GetBytesLimit() uint32 {
v.RLock()
defer v.RUnlock()
if v.state == CongestionStateRTTProbe {
return v.initialThroughput/1000/(1000/defaultRTT) - v.cycleBytesSent
}
return v.cycleBytesLimit
}
func (v *Congestion) RoundTripTime() uint32 {
v.RLock()
defer v.RUnlock()
if v.state == CongestionStateRTTProbe {
return defaultRTT
}
return v.rtt
}
func (v *Congestion) Timeout() uint32 {
v.RLock()
defer v.RUnlock()
if v.state == CongestionStateRTTProbe {
return defaultRTT * 3 / 2
}
return v.rtt * 3 / 2
}