-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrcv.go
244 lines (205 loc) · 5.44 KB
/
rcv.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
package tomtp
import (
"fmt"
"sort"
"sync"
)
//The receiving buffer gets segments that can be out of order. That means, the insert needs
//to store the segments out of order. The remove of segments affect those segments
//that are in order
type RcvInsertStatus uint8
const (
RcvInserted RcvInsertStatus = iota
RcvNothing
RcvOverflow
RcvDuplicate
)
type RcvSegment[T any] struct {
sn uint64
data T
insertedAt uint64
}
type RingBufferRcv[T any] struct {
buffer []*RcvSegment[T]
capacity uint64
targetLimit uint64
currentLimit uint64
minSn uint64
maxSn uint64
size uint64
toAck []uint64
closed bool
mu *sync.Mutex
cond *sync.Cond
}
// NewRingBufferRcv creates a new receiving buffer
func NewRingBufferRcv[T any](limit uint64, capacity uint64) *RingBufferRcv[T] {
var mu sync.Mutex
return &RingBufferRcv[T]{
buffer: make([]*RcvSegment[T], capacity),
capacity: capacity,
targetLimit: 0,
currentLimit: limit,
minSn: 1,
maxSn: 1,
closed: false,
mu: &mu,
cond: sync.NewCond(&mu),
}
}
// Capacity The current total capacity of the receiving buffer. This is the total size.
func (ring *RingBufferRcv[T]) Capacity() uint64 {
return ring.capacity
}
func (ring *RingBufferRcv[T]) Limit() uint64 {
ring.mu.Lock()
defer ring.mu.Unlock()
return ring.currentLimit
}
func (ring *RingBufferRcv[T]) Free() uint64 {
ring.mu.Lock()
defer ring.mu.Unlock()
return ring.currentLimit - ring.size
}
func (ring *RingBufferRcv[T]) Size() uint64 {
ring.mu.Lock()
defer ring.mu.Unlock()
return ring.size
}
func (ring *RingBufferRcv[T]) SetLimit(limit uint64) {
ring.mu.Lock()
defer ring.mu.Unlock()
if limit > ring.capacity {
panic(fmt.Errorf("limit cannot exceed capacity %v > %v", limit, ring.capacity))
}
ring.setLimitInternal(limit)
}
func (ring *RingBufferRcv[T]) setLimitInternal(limit uint64) {
if limit == ring.currentLimit {
// no change
ring.targetLimit = 0
return
}
oldLimit := ring.currentLimit
if ring.currentLimit > limit {
//decrease limit
if (ring.currentLimit - limit) > (ring.maxSn - ring.minSn) {
//need to set targetLimit
ring.targetLimit = limit
ring.currentLimit = ring.maxSn - ring.minSn
} else {
ring.targetLimit = 0
ring.currentLimit = limit
}
} else {
//increase limit
ring.targetLimit = 0
ring.currentLimit = limit
}
newBuffer := make([]*RcvSegment[T], ring.capacity)
for i := uint64(0); i < oldLimit; i++ {
oldSegment := ring.buffer[i]
if oldSegment != nil {
newBuffer[(oldSegment.sn-1)%ring.currentLimit] = oldSegment
}
}
ring.buffer = newBuffer
}
func (ring *RingBufferRcv[T]) Insert(segment *RcvSegment[T]) RcvInsertStatus {
ring.mu.Lock()
defer ring.mu.Unlock()
maxSn := ring.minSn + ring.currentLimit - 1
index := (segment.sn - 1) % ring.currentLimit
ring.addSegmentToAckOrdered(segment.sn)
if segment.sn-1 >= maxSn {
return RcvOverflow
} else if segment.sn < ring.minSn {
//we already delivered this segment, don't add
//but return the ack info again, as acks may
//have been lost
return RcvDuplicate
} else if ring.buffer[index] != nil {
//we may receive a duplicate, don't add
//but return the ack info again, as acks may
//have been lost
return RcvDuplicate
}
ring.buffer[index] = segment
ring.size++
if ring.available() != nil {
ring.cond.Signal()
}
if segment.sn+1 > ring.maxSn {
ring.maxSn = segment.sn + 1
}
return RcvInserted
}
func (ring *RingBufferRcv[T]) addSegmentToAckOrdered(sn uint64) {
// Find the correct position to insert the new sequence number
index := sort.Search(len(ring.toAck), func(i int) bool { return ring.toAck[i] >= sn })
// Insert the sequence number into the correct position
if index < len(ring.toAck) && ring.toAck[index] == sn {
// If sn is already in the list, we don't add it again
return
}
ring.toAck = append(ring.toAck, 0) // Expand the slice by one element
copy(ring.toAck[index+1:], ring.toAck[index:]) // Shift elements to the right
ring.toAck[index] = sn // Insert the new sequence number
}
func (ring *RingBufferRcv[T]) Close() {
ring.mu.Lock()
defer ring.mu.Unlock()
//set flag and just signal waiting goroutines
ring.closed = true
ring.cond.Signal()
}
func (ring *RingBufferRcv[T]) RemoveBlocking() *RcvSegment[T] {
ring.mu.Lock()
defer ring.mu.Unlock()
if ring.available() == nil && !ring.closed {
ring.cond.Wait()
}
return ring.remove()
}
func (ring *RingBufferRcv[T]) Remove() *RcvSegment[T] {
ring.mu.Lock()
defer ring.mu.Unlock()
return ring.remove()
}
func (ring *RingBufferRcv[T]) available() *RcvSegment[T] {
if ring.size == 0 {
return nil
}
return ring.buffer[(ring.minSn-1)%ring.currentLimit]
}
func (ring *RingBufferRcv[T]) remove() *RcvSegment[T] {
//fast path
segment := ring.available()
if segment == nil {
return nil
}
ring.buffer[(ring.minSn-1)%ring.currentLimit] = nil
ring.minSn = segment.sn + 1
ring.size--
//we have not reached target limit, now we have 1 item less, set it
if ring.targetLimit != 0 {
ring.setLimitInternal(ring.targetLimit)
}
return segment
}
func (ring *RingBufferRcv[T]) HasPendingAck() bool {
ring.mu.Lock()
defer ring.mu.Unlock()
return len(ring.toAck) > 0
}
func (ring *RingBufferRcv[T]) NextAck() uint64 {
ring.mu.Lock()
defer ring.mu.Unlock()
if len(ring.toAck) == 0 {
return 0
}
//return next ack
ack := ring.toAck[0]
ring.toAck = ring.toAck[1:]
return ack
}