-
Notifications
You must be signed in to change notification settings - Fork 1
/
send.go
266 lines (211 loc) · 5.67 KB
/
send.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
package cypress
import (
"container/list"
"errors"
"io"
"sync"
"time"
)
// A type use to send a stream of Messages reliably. This type works in
// coordination with Recv to make transport the stream reliably by
// buffering and acking messages.
type Send struct {
OnClosed func()
rw io.ReadWriter
enc *StreamEncoder
buf []byte
closed bool
window int
available int32
reqs *list.List
ackLock sync.Mutex
ackCond *sync.Cond
}
/*
* Note on window size: to maximize throughput, attempt to make this
* equation work: t * w = d * 2 or w = d * 2 / t
*
* t = time between generated messages. Ie, if you're generating 1000
* messages per second, t = 1ms
* w = the window size
* d = the transmission delay of the network
*
* So, t = 0.1ms and d = 0.05ms, then w = 2. This is the minimum window
* size to maximize throughput.
*/
// Given the transmission delay of the network (t) and the
// expected messages per second (mps), calculate the minimum
// window to use to maximize throughput.
func MinimumSendWindow(d time.Duration, mps int) int {
t := time.Duration(mps/1000) * time.Millisecond
return int((d * 20) / t)
}
var (
// Disable windowing, acknowledge each message immediately
NoWindow int = -1
// An average messages/sec rate to calculate against
DefaultMPSRate int = 1000
// A decent minimum window that assures some improved throughput
MinimumWindow = MinimumSendWindow(1*time.Millisecond, DefaultMPSRate)
// A window for use on a fast lan where transmission delay is very small
FastLanWindow = MinimumWindow
// A window for use on a slower lan (cloud infrastructer, across AZ)
SlowLanWindow = MinimumSendWindow(3*time.Millisecond, DefaultMPSRate)
// A window for use over faster internet paths
FastInternetWindow = MinimumSendWindow(10*time.Millisecond, DefaultMPSRate)
// A window for use over slowe internet paths
SlowInternetWindow = MinimumSendWindow(50*time.Millisecond, DefaultMPSRate)
)
// Create a new Send, reading and writing from rw. Window controls
// the size of the ack window to use. See MinimumSendWindow and the Window
// variables for information window sizes. If the window is set to 0, the
// default window size is used.
// NOTE: The window size has a big effect on the throughput of Send, so
// be sure to consider it's value. The larger the window, the higher
// the memory usage and throughput. Fast lans only require a small window
// because there is a very small transmission delay.
func NewSend(rw io.ReadWriteCloser, window int) *Send {
switch window {
case -1:
window = 1
case 0:
window = MinimumWindow
}
s := &Send{
rw: rw,
enc: NewStreamEncoder(rw),
buf: make([]byte, window),
window: window,
available: int32(window),
reqs: list.New(),
}
s.ackCond = sync.NewCond(&s.ackLock)
go s.backgroundAck()
return s
}
// Send the start of a stream to the remote side. This will initialize
// the stream to use Snappy for compression and reliable transmission.
func (s *Send) SendHandshake() error {
hdr := &StreamHeader{
Compression: NONE.Enum(),
Mode: StreamHeader_RELIABLE.Enum(),
}
return s.enc.WriteCustomHeader(hdr)
}
// Send the Message. If there is an error, nack the message so it can
// be sent again later.
func (s *Send) transmit(m *Message) error {
err := s.enc.Receive(m)
if err != nil {
s.sendNacks()
return ErrClosed
}
return nil
}
// Indicates that both sides of the stream have gotten confused and are
// no longer is sync.
var ErrStreamUnsynced = errors.New("stream unsynced")
// Used to track all messages that are currently not ack'd by the remote
// side.
type sendInFlight struct {
req SendRequest
m *Message
}
// Read any acks from the stream and remove them from the requests list.
func (s *Send) readAck() error {
n, err := s.rw.Read(s.buf)
if err != nil {
return err
}
for i := 0; i < n; i++ {
if s.buf[0] != 'k' {
return ErrStreamUnsynced
}
f := s.reqs.Back()
if f == nil {
continue
}
if inf, ok := f.Value.(sendInFlight); ok {
if inf.req != nil {
inf.req.Ack(inf.m)
}
}
s.reqs.Remove(f)
}
s.ackLock.Lock()
s.available += int32(n)
s.ackCond.Signal()
s.ackLock.Unlock()
return nil
}
// Tell the sender about all the messages that it was not able to get
// acks about and thus should be resent.
func (s *Send) sendNacks() {
if s.closed {
return
}
for e := s.reqs.Back(); e != nil; e = e.Prev() {
if inf, ok := e.Value.(sendInFlight); ok {
if inf.req != nil {
inf.req.Nack(inf.m)
}
}
}
s.closed = true
if s.OnClosed != nil {
s.OnClosed()
}
s.ackCond.Signal()
}
// Read acks forever and if there is an error reading acks, nack all
// inflight requests.
func (s *Send) backgroundAck() {
for {
err := s.readAck()
if err != nil {
s.ackLock.Lock()
defer s.ackLock.Unlock()
s.sendNacks()
return
}
}
}
// Send a Message to the remote side
func (s *Send) Receive(m *Message) error {
return s.Send(m, nil)
}
// Indicate that this Send is closed and can not be used
var ErrClosed = errors.New("send closed")
// Send a Message to the remote side. if req is not nil, then
// it will be updated as to the status of m, calling either
// Ack or Nack depending on if things go ok or not.
func (s *Send) Send(m *Message, req SendRequest) error {
s.ackLock.Lock()
defer s.ackLock.Unlock()
if s.closed {
if req != nil {
req.Nack(m)
}
return ErrClosed
}
s.reqs.PushFront(sendInFlight{req, m})
s.available--
err := s.transmit(m)
if err != nil {
return err
}
for s.available == 0 {
if s.closed {
return ErrClosed
}
s.enc.Flush()
s.ackCond.Wait()
}
return nil
}
func (s *Send) Close() error {
return s.enc.Close()
}
func (s *Send) Flush() error {
return s.enc.Flush()
}