class Window
Flow control window for managing HTTP/2 data flow.
Definitions
DEFAULT_CAPACITY = 0xFFFF
When an HTTP/2 connection is first established, new streams are created with an initial flow-control window size of 65,535 octets. The connection flow-control window is also 65,535 octets.
def initialize(capacity = DEFAULT_CAPACITY)
Initialize a new flow control window.
Signature
-
parameter
capacity
Integer
The initial window size, typically from the settings.
Implementation
def initialize(capacity = DEFAULT_CAPACITY)
# This is the main field required:
@available = capacity
# These two fields are primarily used for efficiently sending window updates:
@used = 0
@capacity = capacity
end
def full?
The window is completely full?
Implementation
def full?
@available <= 0
end
def capacity=(value)
When the value of SETTINGS_INITIAL_WINDOW_SIZE changes, a receiver MUST adjust the size of all stream flow-control windows that it maintains by the difference between the new value and the old value.
Implementation
def capacity= value
difference = value - @capacity
# An endpoint MUST treat a change to SETTINGS_INITIAL_WINDOW_SIZE that causes any flow-control window to exceed the maximum size as a connection error of type FLOW_CONTROL_ERROR.
if (@available + difference) > MAXIMUM_ALLOWED_WINDOW_SIZE
raise FlowControlError, "Changing window size by #{difference} caused overflow: #{@available + difference} > #{MAXIMUM_ALLOWED_WINDOW_SIZE}!"
end
@available += difference
@capacity = value
end
def consume(amount)
Consume a specific amount from the available window.
Signature
-
parameter
amount
Integer
The amount to consume from the window.
Implementation
def consume(amount)
@available -= amount
@used += amount
end
def available?
Check if there is available window capacity.
Signature
-
returns
Boolean
True if there is available capacity.
Implementation
def available?
@available > 0
end
def expand(amount)
Expand the window by a specific amount.
Signature
-
parameter
amount
Integer
The amount to expand the window by.
-
raises
FlowControlError
If expansion would cause overflow.
Implementation
def expand(amount)
available = @available + amount
if available > MAXIMUM_ALLOWED_WINDOW_SIZE
raise FlowControlError, "Expanding window by #{amount} caused overflow: #{available} > #{MAXIMUM_ALLOWED_WINDOW_SIZE}!"
end
# puts "expand(#{amount}) @available=#{@available}"
@available += amount
@used -= amount
end
def wanted
Get the amount of window that should be reclaimed.
Signature
-
returns
Integer
The amount of used window space.
Implementation
def wanted
@used
end
def limited?
Check if the window is limited and needs updating.
Signature
-
returns
Boolean
True if available capacity is less than half of total capacity.
Implementation
def limited?
@available < (@capacity / 2)
end
def inspect
Get a string representation of the window.
Signature
-
returns
String
Human-readable window information.
Implementation
def inspect
"\#<#{self.class} available=#{@available} used=#{@used} capacity=#{@capacity}#{limited? ? " limited" : nil}>"
end