-
Notifications
You must be signed in to change notification settings - Fork 4
/
window.go
100 lines (81 loc) · 2.82 KB
/
window.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
package http2
import (
"fmt"
. "github.com/Jxck/color"
. "github.com/Jxck/http2/frame"
. "github.com/Jxck/logger"
"log"
)
func init() {
log.SetFlags(log.Lshortfile)
}
type Window struct {
initialSize int32
currentSize int32
threshold int32
peerInitialSize int32
peerCurrentSize int32
peerThreshold int32
}
func NewWindowDefault() *Window {
return &Window{
initialSize: DEFAULT_INITIAL_WINDOW_SIZE,
currentSize: DEFAULT_INITIAL_WINDOW_SIZE,
threshold: DEFAULT_INITIAL_WINDOW_SIZE/2 + 1,
peerInitialSize: DEFAULT_INITIAL_WINDOW_SIZE,
peerCurrentSize: DEFAULT_INITIAL_WINDOW_SIZE,
peerThreshold: DEFAULT_INITIAL_WINDOW_SIZE/2 + 1,
}
}
func NewWindow(initialWindow, peerInitilaWindow int32) *Window {
return &Window{
initialSize: initialWindow,
currentSize: initialWindow,
threshold: initialWindow/2 + 1,
peerInitialSize: peerInitilaWindow,
peerCurrentSize: peerInitilaWindow,
peerThreshold: peerInitilaWindow/2 + 1,
}
}
func (window *Window) UpdateInitialSize(newInitialWindowSize int32) {
currentInitialWindowSize := window.initialSize
currentWindowSize := window.peerCurrentSize
newWindwoSize := newInitialWindowSize - (window.initialSize - currentWindowSize)
window.peerCurrentSize = newWindwoSize
window.initialSize = newInitialWindowSize
Trace(Brown(`update initial window size
"New WindowSize(%v)" = "New InitialWindowSize(%v)" - ("Current InitialWindow ize(%v)" - "Current WindowSize(%v)")`),
newWindwoSize, newInitialWindowSize, currentInitialWindowSize, currentWindowSize)
}
func (window *Window) Update(windowSizeIncrement int32) {
current := window.currentSize
window.currentSize = current + windowSizeIncrement
Trace(Brown("increment current window size (%v) + increment (%v) = (%v)"), current, windowSizeIncrement, window.currentSize)
}
func (window *Window) UpdatePeer(windowSizeIncrement int32) {
current := window.peerCurrentSize
window.peerCurrentSize = current + windowSizeIncrement
Trace(Brown("increment peer window size (%v) + increment (%v) = (%v)"), current, windowSizeIncrement, window.peerCurrentSize)
}
func (window *Window) Consume(length int32) (update int32) {
window.currentSize -= length
if window.currentSize < window.threshold {
update = window.initialSize - window.currentSize
}
return update
}
func (window *Window) ConsumePeer(length int32) {
current := window.peerCurrentSize
window.peerCurrentSize = current - length
Trace(Brown("consume peer window size (%v) - (%v) = (%v)"), current, length, window.peerCurrentSize)
}
func (window *Window) Consumable(length int32) int32 {
if window.peerCurrentSize < length {
return window.peerCurrentSize
} else {
return length
}
}
func (window *Window) String() string {
return fmt.Sprintf(Yellow("window: curr(%d) - peer(%d)"), window.currentSize, window.peerCurrentSize)
}