class Async::HTTP::Protocol::HTTP2
def call(request)
def call(request) @count += 1 stream = @controller.new_stream response = Response.new(self, stream) body = response.body exception = nil finished = Async::Notification.new waiting = true stream.on(:close) do |error| if waiting if error # If the stream was closed due to an error, we will raise it rather than returning normally. exception = EOFError.new(error) end waiting = false finished.signal else # At this point, we are now expecting two events: data and close. # If we receive close after this point, it's not a request error, but a failure we need to signal to the body. if error body.stop(EOFError.new(error)) else body.finish end end end stream.on(:headers) do |headers| response.assign_headers(headers) # Once we receive the headers, we can return. The body will be read in the background. waiting = false finished.signal end # This is a little bit tricky due to the event handlers. # 1/ Caller invokes `response.stop` which causes `body.write` below to fail. # 2/ We invoke `stream.close(:internal_error)` which eventually triggers `on(:close)` above. # 3/ Error is set to :internal_error which causes us to call `body.stop` a 2nd time. # So, we guard against that, by ensuring that `Writable#stop` only stores the first exception assigned to it. stream.on(:data) do |chunk| begin # If the body is stopped, write will fail... body.write(chunk.to_s) unless chunk.empty? rescue # ... so, we close the stream: stream.close(:internal_error) end end write_request(request, stream) Async.logger.debug(self) {"Request sent, waiting for signal."} finished.wait if exception raise exception end Async.logger.debug(self) {"Stream finished: #{response.inspect}"} return response end