Class: Protocol::HTTP2::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/http2/window.rb

Overview

Flow control window for managing HTTP/2 data flow.

Direct Known Subclasses

LocalWindow

Constant Summary collapse

DEFAULT_CAPACITY =

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.

0xFFFF

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capacity = DEFAULT_CAPACITY) ⇒ Window

Initialize a new flow control window.



15
16
17
18
19
20
21
22
# File 'lib/protocol/http2/window.rb', line 15

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

Instance Attribute Details

#availableObject (readonly)

Returns the value of attribute available.



52
53
54
# File 'lib/protocol/http2/window.rb', line 52

def available
  @available
end

#capacityObject

Returns the value of attribute capacity.



30
31
32
# File 'lib/protocol/http2/window.rb', line 30

def capacity
  @capacity
end

#usedObject (readonly)

Returns the value of attribute used.



29
30
31
# File 'lib/protocol/http2/window.rb', line 29

def used
  @used
end

Instance Method Details

#available?Boolean

Check if there is available window capacity.

Returns:

  • (Boolean)


56
57
58
# File 'lib/protocol/http2/window.rb', line 56

def available?
	@available > 0
end

#consume(amount) ⇒ Object

Consume a specific amount from the available window.



47
48
49
50
# File 'lib/protocol/http2/window.rb', line 47

def consume(amount)
	@available -= amount
	@used += amount
end

#expand(amount) ⇒ Object

Expand the window by a specific amount.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/protocol/http2/window.rb', line 63

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

#full?Boolean

The window is completely full?

Returns:

  • (Boolean)


25
26
27
# File 'lib/protocol/http2/window.rb', line 25

def full?
	@available <= 0
end

#inspectObject Also known as: to_s

Get a string representation of the window.



89
90
91
# File 'lib/protocol/http2/window.rb', line 89

def inspect
	"\#<#{self.class} available=#{@available} used=#{@used} capacity=#{@capacity}#{limited? ? " limited" : nil}>"
end

#limited?Boolean

Check if the window is limited and needs updating.

Returns:

  • (Boolean)


83
84
85
# File 'lib/protocol/http2/window.rb', line 83

def limited?
	@available < (@capacity / 2)
end

#wantedObject

Get the amount of window that should be reclaimed.



77
78
79
# File 'lib/protocol/http2/window.rb', line 77

def wanted
	@used
end