lib/async/http/protocol/http1/server.rb in async-http-0.77.0 vs lib/async/http/protocol/http1/server.rb in async-http-0.78.0
- old
+ new
@@ -5,29 +5,46 @@
# Copyright, 2020, by Igor Sidorov.
# Copyright, 2023, by Thomas Morgan.
# Copyright, 2024, by Anton Zhuravsky.
require_relative "connection"
-require_relative "../../body/finishable"
+require_relative "finishable"
require "console/event/failure"
module Async
module HTTP
module Protocol
module HTTP1
class Server < Connection
+ def initialize(...)
+ super
+
+ @ready = Async::Notification.new
+ end
+
+ def closed!
+ super
+
+ @ready.signal
+ end
+
def fail_request(status)
@persistent = false
write_response(@version, status, {})
write_body(@version, nil)
rescue => error
# At this point, there is very little we can do to recover:
Console::Event::Failure.for(error).emit(self, "Failed to write failure response!", severity: :debug)
end
def next_request
+ # Wait for the connection to become idle before reading the next request:
+ unless idle?
+ @ready.wait
+ end
+
# The default is true.
return unless @persistent
# Read an incoming request:
return unless request = Request.read(self)
@@ -47,11 +64,11 @@
def each(task: Task.current)
task.annotate("Reading #{self.version} requests for #{self.class}.")
while request = next_request
if body = request.body
- finishable = Body::Finishable.new(body)
+ finishable = Finishable.new(body)
request.body = finishable
end
response = yield(request, self)
version = request.version
@@ -124,13 +141,11 @@
write_body(version, nil)
request&.finish
end
+ # Discard or wait for the input body to be consumed:
finishable&.wait
-
- # This ensures we yield at least once every iteration of the loop and allow other fibers to execute.
- task.yield
rescue => error
raise
ensure
body&.close(error)
end