diff --git a/lib/async/http/protocol/http1/server.rb b/lib/async/http/protocol/http1/server.rb index 96e6643..b2daaf6 100644 --- a/lib/async/http/protocol/http1/server.rb +++ b/lib/async/http/protocol/http1/server.rb @@ -10,6 +10,7 @@ require_relative "connection" require_relative "finishable" +require "async/promise" require "console/event/failure" module Async @@ -22,14 +23,14 @@ class Server < Connection def initialize(...) super - @ready = Async::Notification.new + @ready = Async::Promise.new end # Called when the connection is closed, signalling any waiting tasks. def closed(error = nil) super - @ready.signal + @ready.resolve(nil) end # Write a failure response with the given status code. diff --git a/lib/async/http/protocol/http2/response.rb b/lib/async/http/protocol/http2/response.rb index 3da6ab2..1bff410 100644 --- a/lib/async/http/protocol/http2/response.rb +++ b/lib/async/http/protocol/http2/response.rb @@ -6,6 +6,8 @@ require_relative "../response" require_relative "stream" +require "async/promise" + module Async module HTTP module Protocol @@ -20,8 +22,7 @@ def initialize(*) @response = Response.new(self) - @notification = Async::Notification.new - @exception = nil + @ready = Async::Promise.new end attr :response @@ -88,7 +89,7 @@ def receive_initial_headers(headers, end_stream) send_reset_stream(::Protocol::HTTP2::Error::PROTOCOL_ERROR) end - self.notify! + @ready.resolve(nil) return headers end @@ -106,22 +107,9 @@ def receive_interim_headers(status, headers) @response.request.send_interim_response(status, headers) end - # Notify anyone waiting on the response headers to be received (or failure). - def notify! - if notification = @notification - @notification = nil - notification.signal - end - end - # Wait for the headers to be received or for stream reset. def wait - # If you call wait after the headers were already received, it should return immediately: - @notification&.wait - - if @exception - raise @exception - end + @ready.wait rescue ::Protocol::HTTP2::StreamError => error if error.code == ::Protocol::HTTP2::Error::INTERNAL_ERROR raise ::Protocol::HTTP::RemoteError, error.message @@ -139,9 +127,11 @@ def closed(error) @response = nil end - @exception = error - - self.notify! + if error + @ready.reject(error) + else + @ready.resolve(nil) + end end end