module FlowControlled
Provides flow control functionality for HTTP/2 connections and streams. This module implements window-based flow control as defined in RFC 7540.
Definitions
def available_size
Get the available window size for sending data.
Signature
-
returns
Integer
The number of bytes that can be sent.
Implementation
def available_size
@remote_window.available
end
def available_frame_size(maximum_frame_size = self.maximum_frame_size)
This could be negative if the window has been overused due to a change in initial window size.
Implementation
def available_frame_size(maximum_frame_size = self.maximum_frame_size)
available_size = self.available_size
# puts "available_size=#{available_size} maximum_frame_size=#{maximum_frame_size}"
if available_size < maximum_frame_size
return available_size
else
return maximum_frame_size
end
end
def consume_remote_window(frame)
Keep track of the amount of data sent, and fail if is too much.
Implementation
def consume_remote_window(frame)
amount = frame.length
# Frames with zero length with the END_STREAM flag set (that is, an empty DATA frame) MAY be sent if there is no available space in either flow-control window.
if amount.zero? and frame.end_stream?
# It's okay, we can send. No need to consume, it's empty anyway.
elsif amount >= 0 and amount <= @remote_window.available
@remote_window.consume(amount)
else
raise FlowControlError, "Trying to send #{frame.length} bytes, exceeded window size: #{@remote_window.available} (#{@remote_window})"
end
end
def update_local_window(frame)
Update the local window after receiving data.
Signature
-
parameter
frame
Frame
The frame that was received.
Implementation
def update_local_window(frame)
consume_local_window(frame)
request_window_update
end
def consume_local_window(frame)
Consume local window space for a received frame.
Signature
-
parameter
frame
Frame
The frame that consumed window space.
Implementation
def consume_local_window(frame)
# For flow-control calculations, the 9-octet frame header is not counted.
amount = frame.length
@local_window.consume(amount)
end
def request_window_update
Request a window update if the local window is limited.
Implementation
def request_window_update
if @local_window.limited?
self.send_window_update(@local_window.wanted)
end
end
def send_window_update(window_increment)
Notify the remote end that we are prepared to receive more data:
Implementation
def send_window_update(window_increment)
frame = WindowUpdateFrame.new(self.id)
frame.pack window_increment
write_frame(frame)
@local_window.expand(window_increment)
end
def receive_window_update(frame)
Process a received WINDOW_UPDATE frame.
Signature
-
parameter
frame
WindowUpdateFrame
The window update frame to process.
-
raises
ProtocolError
If the window increment is invalid.
Implementation
def receive_window_update(frame)
amount = frame.unpack
if amount != 0
@remote_window.expand(amount)
else
raise ProtocolError, "Invalid window size increment: #{amount}!"
end
end
def window_updated(size)
The window has been expanded by the given amount.
Signature
-
parameter
size
Integer
the maximum amount of data to send.
Implementation
def window_updated(size)
return false
end