-
Notifications
You must be signed in to change notification settings - Fork 9
/
receivequeue.go
282 lines (251 loc) · 6.58 KB
/
receivequeue.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package multipath
import (
"context"
"sync"
"sync/atomic"
"time"
pool "github.com/libp2p/go-buffer-pool"
)
// receiveQueue keeps received frames for the upper layer to read. It is
// maintained as a ring buffer with fixed size. It takes advantage of the fact
// that the frame number is sequential, so when a new frame arrives, it is
// placed at buf[frameNumber % size].
type receiveQueue struct {
readFrameTip uint64
buf []rxFrame
size uint64
// rp stands for read pointer, point to the index of the frame containing
// data yet to be read.
rp uint64
availableFrameChannel chan bool
readNotifyChannel chan bool
readDeadline time.Time
deadlineLock sync.Mutex
closing uint32 // 1 == true, 0 == false -- This is used to "drain" the Queue
fullyClosed uint32 // 1 == true, 0 == false
readLock *sync.Mutex
}
func newReceiveQueue(size int) *receiveQueue {
rq := &receiveQueue{
buf: make([]rxFrame, size),
size: uint64(size),
rp: minFrameNumber % uint64(size), // frame number starts with minFrameNumber, so should the read pointer
availableFrameChannel: make(chan bool, 1),
readNotifyChannel: make(chan bool),
readLock: &sync.Mutex{},
}
return rq
}
func (rq *receiveQueue) add(f *rxFrame, sf *subflow) {
select {
case rq.availableFrameChannel <- true:
default:
}
// Another thing to protect against, is that we might be
// locally blocked on a full receiveQueue. If that is the
// case then we don't want to return instantly from this
// function since that will just case retransmits to fire
// over and over again, causing mass bandwidth loss.
// Instead let's quickly check if we have all of the data we need
// to read, and if we do, hang until we don't have that problem anymore
if rq.isFull() {
for {
var abort bool
select {
case <-rq.readNotifyChannel:
if !rq.isFull() {
abort = true
break
}
}
if abort {
break
}
}
}
select {
case rq.availableFrameChannel <- true:
default:
}
readFrameTip := atomic.LoadUint64(&rq.readFrameTip)
if readFrameTip != 0 {
if readFrameTip > f.fn || readFrameTip == f.fn {
sf.ack(f.fn)
return
}
}
if f.fn > readFrameTip+rq.size && readFrameTip != 0 {
log.Debugf("Near corruption incident?? %v vs the max peek of %v (frametip %d)", f.fn, readFrameTip+rq.size-1, readFrameTip)
return // Nope! this will corrupt the buffer
}
if rq.tryAdd(f) {
sf.ack(f.fn)
return
}
// Protect against the socket being closed
if atomic.LoadUint32(&rq.fullyClosed) == 1 {
pool.Put(f.bytes)
return
}
}
func (rq *receiveQueue) isFull() bool {
printFull := false
for i := uint64(0); i < rq.size; i++ {
expectedFrameNumber := atomic.LoadUint64(&rq.readFrameTip) + i
idx := expectedFrameNumber % rq.size
rq.readLock.Lock()
if rq.buf[idx].fn != expectedFrameNumber {
if printFull {
log.Tracef("receiveQueue is %d%% full! (%d/%d)", int((float32(i) / float32(rq.size) * 100)), i, rq.size)
}
rq.readLock.Unlock()
return false
}
if rq.buf[idx].bytes == nil {
rq.readLock.Unlock()
return false
}
rq.readLock.Unlock()
if i == rq.size/2 {
printFull = true
}
}
return true
}
func (rq *receiveQueue) tryAdd(f *rxFrame) bool {
rq.readLock.Lock()
idx := f.fn % rq.size
if rq.buf[idx].bytes == nil {
// empty slot
rq.buf[idx] = *f
if idx == rq.rp {
select {
case rq.availableFrameChannel <- true:
default:
}
}
rq.readLock.Unlock()
return true
} else if rq.buf[idx].fn == f.fn {
rq.readLock.Unlock()
// retransmission, ignore
log.Tracef("Got a retransmit. for %d", f.fn)
pool.Put(f.bytes)
return true
}
rq.readLock.Unlock()
if idx != 0 {
log.Tracef("Not what I was looking for, I'm looking for frame %v", rq.buf[idx-1].fn+1)
}
return false
}
func (rq *receiveQueue) read(b []byte) (int, error) {
for {
rq.readLock.Lock()
if rq.buf[rq.rp].bytes != nil {
rq.readLock.Unlock()
break
}
rq.readLock.Unlock()
if atomic.LoadUint32(&rq.fullyClosed) == 1 {
return 0, ErrClosed
}
if atomic.LoadUint32(&rq.closing) == 1 {
// if we are closing, then we should check if there is anything left to send
// before sending ErrClosed back upstream, otherwise we may close "early" with
// some data still inside of us!
break
}
if rq.dlExceeded() {
return 0, context.DeadlineExceeded
}
select {
case rq.readNotifyChannel <- true:
default:
}
<-rq.availableFrameChannel
}
rq.readLock.Lock()
defer rq.readLock.Unlock()
totalN := 0
cur := rq.buf[rq.rp].bytes
for cur != nil && totalN < len(b) {
oldFrameTip := atomic.LoadUint64(&rq.readFrameTip)
if (rq.buf[rq.rp].fn != oldFrameTip+1) && (rq.buf[rq.rp].fn != oldFrameTip) && oldFrameTip != 0 {
log.Errorf("receiveQueue buffer corruption detected [%v vs %v] (The crash happened at idx = %d)", rq.buf[rq.rp].fn, oldFrameTip+1, rq.rp)
log.Tracef("All Buffers: ")
for idx, v := range rq.buf {
log.Tracef("\t[%d]fn %d, [%d]byte\n", idx, v.fn, len(v.bytes))
}
rq.close()
return 0, ErrClosed
}
n := copy(b[totalN:], cur)
if n == len(cur) {
log.Tracef("Finished with read frame %d\n", rq.buf[rq.rp].fn)
atomic.StoreUint64(&rq.readFrameTip, rq.buf[rq.rp].fn)
pool.Put(cur)
rq.buf[rq.rp].bytes = nil
rq.rp = (rq.rp + 1) % rq.size
} else {
// The frames in the ring buffer are never overridden, so we can
// safely update the bytes to reflect the next read position.
rq.buf[rq.rp].bytes = cur[n:]
log.Tracef("Partial read frame %d\n", rq.buf[rq.rp].fn)
}
totalN += n
cur = rq.buf[rq.rp].bytes
}
select {
case rq.readNotifyChannel <- true:
default:
}
if totalN == 0 && atomic.LoadUint32(&rq.closing) == 1 {
// close fully
atomic.StoreUint32(&rq.fullyClosed, 1)
return 0, ErrClosed
}
return totalN, nil
}
func (rq *receiveQueue) setReadDeadline(dl time.Time) {
rq.deadlineLock.Lock()
rq.readDeadline = dl
rq.deadlineLock.Unlock()
if !dl.IsZero() {
ttl := dl.Sub(time.Now())
if ttl <= 0 {
for {
abort := false
select {
case rq.availableFrameChannel <- true:
default:
abort = true
}
if abort {
break
}
}
} else {
time.AfterFunc(ttl, func() {
rq.availableFrameChannel <- true
})
}
}
}
func (rq *receiveQueue) dlExceeded() bool {
return !rq.readDeadline.IsZero() && !rq.readDeadline.After(time.Now())
}
func (rq *receiveQueue) close() {
atomic.StoreUint32(&rq.closing, 1)
abort := false
for {
select {
case rq.availableFrameChannel <- true:
default:
abort = true
}
if abort {
break
}
}
}