File tree Expand file tree Collapse file tree 3 files changed +36
-5
lines changed Expand file tree Collapse file tree 3 files changed +36
-5
lines changed Original file line number Diff line number Diff line change 8
8
module Protocol
9
9
module HTTP2
10
10
class Window
11
+ #
12
+ DEFAULT_CAPACITY = 0xFFFF
13
+
11
14
# @param capacity [Integer] The initial window size, typically from the settings.
12
- def initialize ( capacity = 0xFFFF )
15
+ def initialize ( capacity = DEFAULT_CAPACITY )
13
16
# This is the main field required:
14
17
@available = capacity
15
18
@@ -70,14 +73,22 @@ def limited?
70
73
@available < ( @capacity / 2 )
71
74
end
72
75
76
+ def as_json ( ...)
77
+ { used : @used , available : @available , capacity : @capacity }
78
+ end
79
+
80
+ def to_json ( ...)
81
+ as_json . to_json ( ...)
82
+ end
83
+
73
84
def inspect
74
85
"\# <#{ self . class } used=#{ @used } available=#{ @available } capacity=#{ @capacity } >"
75
86
end
76
87
end
77
88
78
89
# This is a window which efficiently maintains a desired capacity.
79
90
class LocalWindow < Window
80
- def initialize ( capacity = 0xFFFF , desired : nil )
91
+ def initialize ( capacity = DEFAULT_CAPACITY , desired : nil )
81
92
super ( capacity )
82
93
83
94
@desired = desired
@@ -90,17 +101,22 @@ def wanted
90
101
# We must send an update which allows at least @desired bytes to be sent.
91
102
( @desired - @capacity ) + @used
92
103
else
93
- @used
104
+ super
94
105
end
95
106
end
96
107
97
108
def limited?
98
109
if @desired
99
- @available < @desired
110
+ # Do not send window updates until we are less than half the desired capacity:
111
+ @available < ( @desired / 2 )
100
112
else
101
113
super
102
114
end
103
115
end
116
+
117
+ def as_json ( ...)
118
+ super . merge ( desired : @desired )
119
+ end
104
120
end
105
121
106
122
# The WINDOW_UPDATE frame is used to implement flow control.
Original file line number Diff line number Diff line change 55
55
expect ( window . wanted ) . to be == 200
56
56
end
57
57
end
58
+
59
+ with "#limited?" do
60
+ it "becomes limited after half the capacity is consumed" do
61
+ expect ( window ) . not . to be ( :limited? )
62
+
63
+ # Consume a little more than half:
64
+ window . consume ( window . capacity / 2 + 2 )
65
+
66
+ expect ( window ) . to be ( :limited? )
67
+ end
68
+ end
58
69
end
Original file line number Diff line number Diff line change @@ -203,8 +203,12 @@ def before
203
203
expect ( frame ) . to be_a Protocol ::HTTP2 ::WindowUpdateFrame
204
204
end
205
205
206
- expect ( client ) . to receive ( :receive_window_update )
206
+ expect ( client ) . to receive ( :receive_window_update ) . twice
207
207
208
+ stream . send_data ( "*" * client . available_size )
209
+ expect ( server . read_frame ) . to be_a Protocol ::HTTP2 ::DataFrame
210
+
211
+ frame = client . read_frame
208
212
expect ( frame ) . to be_a ( Protocol ::HTTP2 ::WindowUpdateFrame )
209
213
expect ( frame ) . to be ( :connection? ) # stream_id = 0
210
214
You can’t perform that action at this time.
0 commit comments