Module: Async::HTTP::Protocol::HTTP2::Connection

Included in:
Client, Server
Defined in:
lib/async/http/protocol/http2/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



118
119
120
# File 'lib/async/http/protocol/http2/connection.rb', line 118

def count
  @count
end

#promisesObject (readonly)

Returns the value of attribute promises.



112
113
114
# File 'lib/async/http/protocol/http2/connection.rb', line 112

def promises
  @promises
end

#streamObject (readonly)

Returns the value of attribute stream.



54
55
56
# File 'lib/async/http/protocol/http2/connection.rb', line 54

def stream
  @stream
end

Instance Method Details

#as_jsonObject



46
47
48
# File 'lib/async/http/protocol/http2/connection.rb', line 46

def as_json(...)
	to_s
end

#close(error = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/async/http/protocol/http2/connection.rb', line 68

def close(error = nil)
	# Ensure the reader task is stopped.
	if @reader
		reader = @reader
		@reader = nil
		reader.stop
	end
	
	super
end

#concurrencyObject



120
121
122
# File 'lib/async/http/protocol/http2/connection.rb', line 120

def concurrency
	self.maximum_concurrent_streams
end

#http1?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/async/http/protocol/http2/connection.rb', line 56

def http1?
	false
end

#http2?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/async/http/protocol/http2/connection.rb', line 60

def http2?
	true
end

#initializeObject



28
29
30
31
32
33
34
35
36
# File 'lib/async/http/protocol/http2/connection.rb', line 28

def initialize(*)
	super
	
	@count = 0
	@reader = nil
	
	# Writing multiple frames at the same time can cause odd problems if frames are only partially written. So we use a semaphore to ensure frames are written in their entirety.
	@write_frame_guard = Async::Semaphore.new(1)
end

#peerObject



114
115
116
# File 'lib/async/http/protocol/http2/connection.rb', line 114

def peer
	@stream.io
end

#read_in_background(parent: Task.current) ⇒ Object

Raises:

  • (RuntimeError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/async/http/protocol/http2/connection.rb', line 79

def read_in_background(parent: Task.current)
	raise RuntimeError, "Connection is closed!" if closed?
	
	parent.async(transient: true) do |task|
		@reader = task
		
		task.annotate("#{version} reading data for #{self.class}.")
		
		# We don't need to defer stop here as this is already a transient task (ignores stop):
		begin
			while !self.closed?
				self.consume_window
				self.read_frame
			end
		rescue Async::Stop, ::IO::TimeoutError, ::Protocol::HTTP2::GoawayError => error
			# Error is raised if a response is actively reading from the
			# connection. The connection is silently closed if GOAWAY is
			# received outside the request/response cycle.
		rescue SocketError, IOError, EOFError, Errno::ECONNRESET, Errno::EPIPE => ignored_error
			# Ignore.
		rescue => error
			# Every other error.
		ensure
			# Don't call #close twice.
			if @reader
				@reader = nil
				
				self.close(error)
			end
		end
	end
end

#reusable?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/async/http/protocol/http2/connection.rb', line 129

def reusable?
	!self.closed?
end

#start_connectionObject



64
65
66
# File 'lib/async/http/protocol/http2/connection.rb', line 64

def start_connection
	@reader || read_in_background
end

#synchronize(&block) ⇒ Object



38
39
40
# File 'lib/async/http/protocol/http2/connection.rb', line 38

def synchronize(&block)
	@write_frame_guard.acquire(&block)
end

#to_jsonObject



50
51
52
# File 'lib/async/http/protocol/http2/connection.rb', line 50

def to_json(...)
	as_json.to_json(...)
end

#to_sObject



42
43
44
# File 'lib/async/http/protocol/http2/connection.rb', line 42

def to_s
	"\#<#{self.class} #{@count} requests, #{@streams.count} active streams>"
end

#versionObject



133
134
135
# File 'lib/async/http/protocol/http2/connection.rb', line 133

def version
	VERSION
end

#viable?Boolean

Can we use this connection to make requests?

Returns:

  • (Boolean)


125
126
127
# File 'lib/async/http/protocol/http2/connection.rb', line 125

def viable?
	@stream&.readable?
end