Skip to content

Commit 8bb02eb

Browse files
committed
WIP.
1 parent 0290218 commit 8bb02eb

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

lib/protocol/http2/window_update_frame.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
module Protocol
99
module HTTP2
1010
class Window
11+
#
12+
DEFAULT_CAPACITY = 0xFFFF
13+
1114
# @param capacity [Integer] The initial window size, typically from the settings.
12-
def initialize(capacity = 0xFFFF)
15+
def initialize(capacity = DEFAULT_CAPACITY)
1316
# This is the main field required:
1417
@available = capacity
1518

@@ -70,14 +73,22 @@ def limited?
7073
@available < (@capacity / 2)
7174
end
7275

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+
7384
def inspect
7485
"\#<#{self.class} used=#{@used} available=#{@available} capacity=#{@capacity}>"
7586
end
7687
end
7788

7889
# This is a window which efficiently maintains a desired capacity.
7990
class LocalWindow < Window
80-
def initialize(capacity = 0xFFFF, desired: nil)
91+
def initialize(capacity = DEFAULT_CAPACITY, desired: nil)
8192
super(capacity)
8293

8394
@desired = desired
@@ -90,17 +101,22 @@ def wanted
90101
# We must send an update which allows at least @desired bytes to be sent.
91102
(@desired - @capacity) + @used
92103
else
93-
@used
104+
super
94105
end
95106
end
96107

97108
def limited?
98109
if @desired
99-
@available < @desired
110+
# Do not send window updates until we are less than half the desired capacity:
111+
@available < (@desired / 2)
100112
else
101113
super
102114
end
103115
end
116+
117+
def as_json(...)
118+
super.merge(desired: @desired)
119+
end
104120
end
105121

106122
# The WINDOW_UPDATE frame is used to implement flow control.

test/protocol/http2/window.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,15 @@
5555
expect(window.wanted).to be == 200
5656
end
5757
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
5869
end

test/protocol/http2/window_update_frame.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,12 @@ def before
203203
expect(frame).to be_a Protocol::HTTP2::WindowUpdateFrame
204204
end
205205

206-
expect(client).to receive(:receive_window_update)
206+
expect(client).to receive(:receive_window_update).twice
207207

208+
stream.send_data("*" * client.available_size)
209+
expect(server.read_frame).to be_a Protocol::HTTP2::DataFrame
210+
211+
frame = client.read_frame
208212
expect(frame).to be_a(Protocol::HTTP2::WindowUpdateFrame)
209213
expect(frame).to be(:connection?) # stream_id = 0
210214

0 commit comments

Comments
 (0)