-
Notifications
You must be signed in to change notification settings - Fork 0
/
synced_buffer.go
193 lines (167 loc) · 4.94 KB
/
synced_buffer.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
package ftcp
import (
"sync"
"github.com/akmistry/go-util/ringbuffer"
"github.com/akmistry/ftcp/pb"
)
type SyncedBuffer struct {
// Sequence number of the first byte in the buffer
startSeq uint64
// Next sequence number to send
// This is NOT synchonised with the failover server. It can be re-created
// on failover by retransmitting starting at |startSeq|.
nextSeq uint64
rb *ringbuffer.RingBuffer
lock sync.Mutex
}
func NewSyncedBuffer(initSeq uint32, initWindowSize int) *SyncedBuffer {
b := &SyncedBuffer{
startSeq: uint64(initSeq),
nextSeq: uint64(initSeq),
rb: ringbuffer.NewRingBuffer(make([]byte, initWindowSize)),
}
return b
}
func (b *SyncedBuffer) StartSeq() uint64 {
return b.startSeq
}
// Sequence number of one past the end of the buffer
func (b *SyncedBuffer) EndSeq() uint64 {
return b.startSeq + uint64(b.rb.Len())
}
func (b *SyncedBuffer) NextSendSeq() uint64 {
return b.nextSeq
}
func (b *SyncedBuffer) ResetNextSeq() {
b.nextSeq = b.startSeq
}
func (b *SyncedBuffer) HasUnsentData() bool {
return b.nextSeq < b.EndSeq()
}
func (b *SyncedBuffer) Len() int {
return b.rb.Len()
}
func (b *SyncedBuffer) Cap() int {
return b.rb.Cap()
}
func (b *SyncedBuffer) Free() int {
return b.Cap() - b.Len()
}
func (b *SyncedBuffer) Ack(ack uint32) bool {
diff := ack - uint32(b.startSeq)
if diff > uint32(b.rb.Len()) {
LogDebug("ACK %d out of sent range [%d, %d)", ack, b.startSeq, b.EndSeq())
return false
}
b.startSeq += uint64(diff)
if b.nextSeq < b.startSeq {
b.ResetNextSeq()
}
b.rb.Consume(int(diff))
return diff > 0
}
func (b *SyncedBuffer) SendData(numBytes int) {
b.nextSeq += uint64(numBytes)
if b.nextSeq > b.EndSeq() {
panic("b.nextSeq > b.EndSeq()")
}
}
func (b *SyncedBuffer) Append(buf []byte) int {
n, err := b.rb.Append(buf)
if err != nil && err != ringbuffer.ErrBufferFull {
// This should never happen.
panic(err)
}
return n
}
func (b *SyncedBuffer) Fetch(buf []byte, offset int) int {
if offset < 0 {
panic("offset < 0")
}
return b.rb.Fetch(buf, offset)
}
func (b *SyncedBuffer) Consume(n int) {
if n > b.Len() {
panic("n > b.Len()")
}
b.startSeq += uint64(n)
b.rb.Consume(n)
}
func (b *SyncedBuffer) FillStateUpdate(update *pb.BufferStateUpdate) {
update.StartSeq = b.startSeq
update.BufLen = uint32(b.rb.Len())
}
func (b *SyncedBuffer) UpdateState(req *pb.BufferStateUpdate) {
if req.StartSeq != 0 {
if req.StartSeq > b.startSeq {
// Sender is ahead on ACKs (has received them from the remote end), or
// recv buffer has been read by the client. Just fast-forward and drop data.
diff := req.StartSeq - b.startSeq
b.startSeq = req.StartSeq
// Drop data from the buffer
if diff > uint64(b.rb.Len()) {
diff = uint64(b.rb.Len())
}
b.rb.Consume(int(diff))
if b.nextSeq < b.startSeq {
b.nextSeq = b.startSeq
}
} else {
// Sender is behind. Our buffer has data ack'd/read, so do nothing.
// Expect the sender to fast-forward when it gets our state.
}
}
if req.AppendSeq != 0 {
currEndSeq := b.EndSeq()
if req.AppendSeq <= currEndSeq {
// Sender is behind us. Drop any data we already have, and append the
// rest.
diff := currEndSeq - req.AppendSeq
if diff < uint64(len(req.AppendData)) {
n, _ := b.rb.Append(req.AppendData[int(diff):])
if n < (len(req.AppendData) - int(diff)) {
// This could happen if the start sequence number has gone out of
// sync and we're far behind, causing appended data to overflow the
// ring buffer. Panic for now, since we synchornise the sequence
// number above, and therefore this represents a programming error.
// TODO: Make this more robust.
panic("n < (len(req.AppendData) - int(diff))")
}
}
} else {
// We're behind. Need to have the sender send us more data. We could
// probably store the received data and have the sender send us the gap,
// but that would make the logic more complicated. For now, just drop the
// received data, and expect the sender to give us everything starting at
// the end of our buffer.
if req.AppendSeq > currEndSeq {
LogWarn("SyncedBuffer: TOO FAR BEHIND, MORE DATA NEEDED!!!")
}
}
}
}
func (b *SyncedBuffer) Sync(req *pb.BufferStateUpdate, reply *pb.BufferStateUpdate) {
// Assume message ID and key have already been verified.
// |reply| is assumed to be empty.
b.UpdateState(req)
// Always tell the sender what our state is, so it can synchronise.
b.FillStateUpdate(reply)
// If we are too far ahead of the sender, send them our data so it can catch
// up.
if req.StartSeq != 0 {
senderSeqEnd := req.StartSeq + uint64(req.BufLen)
currEndSeq := b.EndSeq()
if senderSeqEnd < currEndSeq {
diff := currEndSeq - senderSeqEnd
if diff > uint64(b.rb.Len()) {
diff = uint64(b.rb.Len())
}
reply.AppendSeq = currEndSeq - diff
reply.AppendData = make([]byte, int(diff))
n := b.rb.Fetch(reply.AppendData, b.rb.Len()-int(diff))
if n != int(diff) {
panic("n != int(diff)")
}
}
}
}