forked from SlyMarbo/spdy
/
flow.go
348 lines (300 loc) · 8.63 KB
/
flow.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright 2013 Jamie Hall. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package spdy3
import (
"errors"
"sync"
"github.com/SlyMarbo/spdy/common"
"github.com/SlyMarbo/spdy/spdy3/frames"
)
type DefaultFlowControl uint32
func (f DefaultFlowControl) InitialWindowSize() uint32 {
return uint32(f)
}
func (f DefaultFlowControl) ReceiveData(_ common.StreamID, initialWindowSize uint32, newWindowSize int64) uint32 {
if newWindowSize < (int64(initialWindowSize) / 2) {
return uint32(int64(initialWindowSize) - newWindowSize)
}
return 0
}
// flowControl is used by Streams to ensure that
// they abide by SPDY's flow control rules. For
// versions of SPDY before 3, this has no effect.
type flowControl struct {
sync.Mutex
conn *Conn
stream common.Stream
streamID common.StreamID
output chan<- common.Frame
initialWindow uint32
transferWindow int64
sent uint32
buffer [][]byte
constrained bool
initialWindowThere uint32
transferWindowThere int64
flowControl common.FlowControl
waiting chan bool
}
// AddFlowControl initialises flow control for
// the Stream. If the Stream is running at an
// older SPDY version than SPDY/3, the flow
// control has no effect. Multiple calls to
// AddFlowControl are safe.
func (s *PushStream) AddFlowControl(f common.FlowControl) {
if s.flow != nil {
return
}
s.flow = new(flowControl)
s.flow.conn = s.conn
s.conn.initialWindowSizeLock.Lock()
initialWindow := s.conn.initialWindowSize
s.conn.initialWindowSizeLock.Unlock()
s.flow.streamID = s.streamID
s.flow.output = s.output
s.flow.buffer = make([][]byte, 0, 10)
s.flow.initialWindow = initialWindow
s.flow.transferWindow = int64(initialWindow)
s.flow.stream = s
s.flow.flowControl = f
s.flow.initialWindowThere = f.InitialWindowSize()
s.flow.transferWindowThere = int64(s.flow.transferWindowThere)
}
// AddFlowControl initialises flow control for
// the Stream. If the Stream is running at an
// older SPDY version than SPDY/3, the flow
// control has no effect. Multiple calls to
// AddFlowControl are safe.
func (s *RequestStream) AddFlowControl(f common.FlowControl) {
if s.flow != nil {
return
}
s.flow = new(flowControl)
s.flow.conn = s.conn
s.conn.initialWindowSizeLock.Lock()
initialWindow := s.conn.initialWindowSize
s.conn.initialWindowSizeLock.Unlock()
s.flow.streamID = s.streamID
s.flow.output = s.output
s.flow.buffer = make([][]byte, 0, 10)
s.flow.initialWindow = initialWindow
s.flow.transferWindow = int64(initialWindow)
s.flow.stream = s
s.flow.flowControl = f
s.flow.initialWindowThere = f.InitialWindowSize()
s.flow.transferWindowThere = int64(s.flow.initialWindowThere)
}
// AddFlowControl initialises flow control for
// the Stream. If the Stream is running at an
// older SPDY version than SPDY/3, the flow
// control has no effect. Multiple calls to
// AddFlowControl are safe.
func (s *ResponseStream) AddFlowControl(f common.FlowControl) {
if s.flow != nil {
return
}
s.flow = new(flowControl)
s.flow.conn = s.conn
s.conn.initialWindowSizeLock.Lock()
initialWindow := s.conn.initialWindowSize
s.conn.initialWindowSizeLock.Unlock()
s.flow.streamID = s.streamID
s.flow.output = s.output
s.flow.buffer = make([][]byte, 0, 10)
s.flow.initialWindow = initialWindow
s.flow.transferWindow = int64(initialWindow)
s.flow.stream = s
s.flow.flowControl = f
s.flow.initialWindowThere = f.InitialWindowSize()
s.flow.transferWindowThere = int64(s.flow.initialWindowThere)
}
// CheckInitialWindow is used to handle the race
// condition where the flow control is initialised
// before the server has received any updates to
// the initial tranfer window sent by the client.
//
// The transfer window is updated retroactively,
// if necessary.
func (f *flowControl) CheckInitialWindow() {
if f.stream == nil || f.stream.Conn() == nil {
return
}
f.conn.initialWindowSizeLock.Lock()
newWindow := f.conn.initialWindowSize
f.conn.initialWindowSizeLock.Unlock()
if f.initialWindow != newWindow {
if f.initialWindow > newWindow {
f.transferWindow = int64(newWindow - f.sent)
} else if f.initialWindow < newWindow {
f.transferWindow += int64(newWindow - f.initialWindow)
}
if f.transferWindow <= 0 {
f.constrained = true
}
f.initialWindow = newWindow
}
}
// Close nils any references held by the flowControl.
func (f *flowControl) Close() {
f.buffer = nil
f.stream = nil
}
// Flush is used to send buffered data to
// the connection, if the transfer window
// will allow. Flush does not guarantee
// that any or all buffered data will be
// sent with a single flush.
func (f *flowControl) Flush() {
f.CheckInitialWindow()
if !f.constrained || f.transferWindow == 0 {
return
}
out := make([]byte, 0, f.transferWindow)
left := f.transferWindow
for i := 0; i < len(f.buffer); i++ {
if l := int64(len(f.buffer[i])); l <= left {
out = append(out, f.buffer[i]...)
left -= l
f.buffer = f.buffer[1:]
} else {
out = append(out, f.buffer[i][:left]...)
f.buffer[i] = f.buffer[i][left:]
left = 0
}
if left == 0 {
break
}
}
f.transferWindow -= int64(len(out))
if f.transferWindow > 0 {
f.constrained = false
debug.Printf("Stream %d is no longer constrained.\n", f.streamID)
}
dataFrame := new(frames.DATA)
dataFrame.StreamID = f.streamID
dataFrame.Data = out
f.output <- dataFrame
}
// Paused indicates whether there is data buffered.
// A Stream should not be closed until after the
// last data has been sent and then Paused returns
// false.
func (f *flowControl) Paused() bool {
f.CheckInitialWindow()
return f.constrained
}
// Receive is called when data is received from
// the other endpoint. This ensures that they
// conform to the transfer window, regrows the
// window, and sends errors if necessary.
func (f *flowControl) Receive(data []byte) {
// The transfer window shouldn't already be negative.
if f.transferWindowThere < 0 {
rst := new(frames.RST_STREAM)
rst.StreamID = f.streamID
rst.Status = common.RST_STREAM_FLOW_CONTROL_ERROR
f.output <- rst
}
// Update the window.
f.transferWindowThere -= int64(len(data))
// Regrow the window if it's half-empty.
delta := f.flowControl.ReceiveData(f.streamID, f.initialWindowThere, f.transferWindowThere)
if delta != 0 {
grow := new(frames.WINDOW_UPDATE)
grow.StreamID = f.streamID
grow.DeltaWindowSize = delta
f.output <- grow
f.transferWindowThere += int64(grow.DeltaWindowSize)
}
}
// UpdateWindow is called when an UPDATE_WINDOW frame is received,
// and performs the growing of the transfer window.
func (f *flowControl) UpdateWindow(deltaWindowSize uint32) error {
f.Lock()
defer f.Unlock()
if int64(deltaWindowSize)+f.transferWindow > common.MAX_TRANSFER_WINDOW_SIZE {
return errors.New("Error: WINDOW_UPDATE delta window size overflows transfer window size.")
}
// Grow window and flush queue.
debug.Printf("Flow: Growing window in stream %d by %d bytes.\n", f.streamID, deltaWindowSize)
f.transferWindow += int64(deltaWindowSize)
f.Flush()
select {
case f.waiting <- true:
default:
}
return nil
}
// Wait blocks until any buffered data has been sent.
// This may involve waiting for a window update from
// the peer.
func (f *flowControl) Wait() error {
f.Lock()
f.Flush()
if !f.Paused() {
f.Unlock()
return nil
}
if f.waiting != nil {
f.Unlock()
return errors.New("waiting for flow control twice")
}
f.waiting = make(chan bool)
f.Unlock()
for {
<-f.waiting
f.Flush()
if !f.Paused() {
return nil
}
}
}
// Write is used to send data to the connection. This
// takes care of the windowing. Although data may be
// buffered, rather than actually sent, this is not
// visible to the caller.
func (f *flowControl) Write(data []byte) (int, error) {
l := len(data)
if l == 0 {
return 0, nil
}
if f.buffer == nil || f.stream == nil {
return 0, errors.New("Error: Stream closed.")
}
// Transfer window processing.
f.CheckInitialWindow()
if f.constrained {
f.Flush()
}
f.Lock()
var window uint32
if f.transferWindow < 0 {
window = 0
} else {
window = uint32(f.transferWindow)
}
constrained := false
sending := uint32(len(data))
if sending > window {
sending = window
constrained = true
}
f.sent += sending
f.transferWindow -= int64(sending)
if constrained {
f.buffer = append(f.buffer, data[window:])
data = data[:window]
f.constrained = true
debug.Printf("Stream %d is now constrained.\n", f.streamID)
}
f.Unlock()
if len(data) == 0 {
return l, nil
}
dataFrame := new(frames.DATA)
dataFrame.StreamID = f.streamID
dataFrame.Data = data
f.output <- dataFrame
return l, nil
}