module Async::HTTP::Protocol::HTTP2::Connection

def as_json(...)

def as_json(...)
	to_s
end

def close(error = nil)

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

def concurrency

def concurrency
	self.maximum_concurrent_streams
end

def http1?

def http1?
	false
end

def http2?

def http2?
	true
end

def initialize(*)

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

def peer

def peer
	@stream.io
end

def read_in_background(parent: Task.current)

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
				self.close(error)
			end
		end
	end
end

def reusable?

def reusable?
	!self.closed?
end

def start_connection

def start_connection
	@reader || read_in_background
end

def synchronize(&block)

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

def to_json(...)

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

def to_s

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

def version

def version
	VERSION
end

def viable?

Can we use this connection to make requests?
def viable?
	@stream&.readable?
end