forked from urnetwork/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer_rtt.go
More file actions
222 lines (185 loc) · 4.56 KB
/
transfer_rtt.go
File metadata and controls
222 lines (185 loc) · 4.56 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package connect
import (
"fmt"
"sync"
"time"
"container/heap"
"github.com/golang/glog"
"github.com/urnetwork/connect/protocol"
)
type rttWindowItem struct {
sendTime time.Time
receiveTime time.Time
rtt time.Duration
heapIndex int
}
func newRttWindowItem(sendTime time.Time, receiveTime time.Time) *rttWindowItem {
return &rttWindowItem{
sendTime: sendTime,
receiveTime: receiveTime,
rtt: receiveTime.Sub(sendTime),
}
}
type RttWindow struct {
windowTimeout time.Duration
rttScale float32
minScaledRtt time.Duration
maxScaledRtt time.Duration
stateLock sync.Mutex
window []*rttWindowItem
windowTailIndex int
windowHeadIndex int
rtts *rttHeap
}
func NewRttWindow(
windowSize int,
windowTimeout time.Duration,
rttScale float32,
minScaledRtt time.Duration,
maxScaledRtt time.Duration,
) *RttWindow {
if windowSize == 0 {
panic(fmt.Errorf("Window size must non-zero: %d", windowSize))
}
window := make([]*rttWindowItem, windowSize)
return &RttWindow{
windowTimeout: windowTimeout,
rttScale: rttScale,
minScaledRtt: minScaledRtt,
maxScaledRtt: maxScaledRtt,
window: window,
windowTailIndex: 0,
windowHeadIndex: 0,
rtts: newRttHeap(),
}
}
// must be called inside the state lock
func (self *RttWindow) coalesce(windowTime time.Time) {
windowStartTime := windowTime.Add(-self.windowTimeout)
for self.windowTailIndex != self.windowHeadIndex {
item := self.window[self.windowTailIndex]
if !item.receiveTime.Before(windowStartTime) {
break
}
self.rtts.Remove(item)
self.window[self.windowTailIndex] = nil
self.windowTailIndex = (self.windowTailIndex + 1) % len(self.window)
}
}
func (self *RttWindow) OpenTag() *protocol.Tag {
return self.openTag(time.Now())
}
func (self *RttWindow) openTag(sendTime time.Time) *protocol.Tag {
// sendTime
return &protocol.Tag{
SendTime: uint64(sendTime.UnixMilli()),
}
}
func (self *RttWindow) CloseTag(tag *protocol.Tag) {
self.closeTag(tag, time.Now())
}
func (self *RttWindow) closeTag(tag *protocol.Tag, receiveTime time.Time) {
sendTime := time.UnixMilli(int64(tag.SendTime))
if receiveTime.Before(sendTime) {
// ignore
return
}
self.stateLock.Lock()
defer self.stateLock.Unlock()
self.coalesce(receiveTime)
item := newRttWindowItem(
sendTime,
receiveTime,
)
self.rtts.Add(item)
if replaceItem := self.window[self.windowHeadIndex]; replaceItem != nil {
self.rtts.Remove(replaceItem)
}
self.window[self.windowHeadIndex] = item
self.windowHeadIndex = (self.windowHeadIndex + 1) % len(self.window)
if self.windowTailIndex == self.windowHeadIndex {
self.windowTailIndex = (self.windowTailIndex + 1) % len(self.window)
}
}
// min(max of window * scale, overall max)
func (self *RttWindow) ScaledRtt() time.Duration {
return self.scaledRtt(time.Now())
}
func (self *RttWindow) scaledRtt(sendTime time.Time) time.Duration {
self.stateLock.Lock()
defer self.stateLock.Unlock()
self.coalesce(sendTime)
useRtt := self.rtts.MeanRtt()
scaledRtt := min(
max(
time.Duration(float32(useRtt/time.Millisecond)*self.rttScale)*time.Millisecond,
self.minScaledRtt,
),
self.maxScaledRtt,
)
glog.V(2).Infof("[rtt]scaled=%dms\n", scaledRtt/time.Millisecond)
return scaledRtt
}
type rttHeap struct {
items []*rttWindowItem
netRtt time.Duration
}
// `heap` is a min heap
func newRttHeap() *rttHeap {
h := &rttHeap{
items: []*rttWindowItem{},
netRtt: time.Duration(0),
}
heap.Init(h)
return h
}
func (self *rttHeap) Add(item *rttWindowItem) {
heap.Push(self, item)
self.netRtt += item.rtt
}
func (self *rttHeap) Remove(item *rttWindowItem) {
heap.Remove(self, item.heapIndex)
self.netRtt -= item.rtt
}
func (self *rttHeap) MinRtt() time.Duration {
n := len(self.items)
if n == 0 {
return time.Duration(0)
}
maxItem := self.items[n-1]
return maxItem.rtt
}
func (self *rttHeap) MeanRtt() time.Duration {
n := len(self.items)
if n == 0 {
return 0
}
return self.netRtt / time.Duration(n)
}
// `heap.Interface`
func (self *rttHeap) Len() int {
return len(self.items)
}
func (self *rttHeap) Less(i, j int) bool {
return self.items[i].rtt < self.items[j].rtt
}
func (self *rttHeap) Swap(i, j int) {
a := self.items[i]
b := self.items[j]
b.heapIndex = i
self.items[i] = b
a.heapIndex = j
self.items[j] = a
}
func (self *rttHeap) Push(x any) {
item := x.(*rttWindowItem)
item.heapIndex = len(self.items)
self.items = append(self.items, item)
}
func (self *rttHeap) Pop() any {
n := len(self.items)
item := self.items[n-1]
self.items[n-1] = nil
self.items = self.items[0 : n-1]
return item
}