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

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
				@reader = nil
				
				self.close(error)
			end
		end
	end
end