-
Notifications
You must be signed in to change notification settings - Fork 3
/
flow_controller.go
65 lines (51 loc) · 1.31 KB
/
flow_controller.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
package main
import (
"fmt"
)
type WindowSize struct {
current uint32
delta int32
}
func (ws WindowSize) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%d", ws.current)), nil
}
type FlowController struct {
InitialWindowSize uint32
ConnectionWindowSize uint32
StreamWindowSize map[uint32]uint32
}
func (fc *FlowController) UpdateConnectionWindow(size uint32) WindowSize {
original := fc.ConnectionWindowSize
fc.ConnectionWindowSize += size
if fc.ConnectionWindowSize < 0 {
fc.ConnectionWindowSize = 0
}
return WindowSize{
fc.ConnectionWindowSize,
int32(fc.ConnectionWindowSize - original),
}
}
func (fc *FlowController) UpdateStreamWindow(streamID uint32, size uint32) WindowSize {
_, ok := fc.StreamWindowSize[streamID]
if !ok {
fc.StreamWindowSize[streamID] = fc.InitialWindowSize
}
original := fc.StreamWindowSize[streamID]
fc.StreamWindowSize[streamID] += size
if fc.StreamWindowSize[streamID] < 0 {
fc.StreamWindowSize[streamID] = 0
}
return WindowSize{
fc.StreamWindowSize[streamID],
int32(fc.StreamWindowSize[streamID] - original),
}
}
func NewFlowController() *FlowController {
initSize := 65535
fc := &FlowController{
InitialWindowSize: uint32(initSize),
ConnectionWindowSize: uint32(initSize),
StreamWindowSize: map[uint32]uint32{},
}
return fc
}