Skip to content

Commit c7c1851

Browse files
committed
WIP
1 parent 2f3180a commit c7c1851

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

async-http.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ Gem::Specification.new do |spec|
2828
spec.add_dependency "async-pool", "~> 0.7"
2929
spec.add_dependency "io-endpoint", "~> 0.11"
3030
spec.add_dependency "io-stream", "~> 0.4"
31-
spec.add_dependency "protocol-http", "~> 0.35"
32-
spec.add_dependency "protocol-http1", "~> 0.20"
31+
spec.add_dependency "protocol-http", "~> 0.36"
32+
spec.add_dependency "protocol-http1", "~> 0.23"
3333
spec.add_dependency "protocol-http2", "~> 0.18"
3434
spec.add_dependency "traces", ">= 0.10"
3535
end

lib/async/http/body/finishable.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,25 @@
99
module Async
1010
module HTTP
1111
module Body
12+
# Keeps track of whether a body is being read, and if so, waits for it to be closed.
1213
class Finishable < ::Protocol::HTTP::Body::Wrapper
1314
def initialize(body)
1415
super(body)
1516

1617
@closed = Async::Variable.new
1718
@error = nil
19+
20+
@reading = false
21+
end
22+
23+
def reading?
24+
@reading
25+
end
26+
27+
def read
28+
@reading = true
29+
30+
super
1831
end
1932

2033
def close(error = nil)
@@ -27,7 +40,11 @@ def close(error = nil)
2740
end
2841

2942
def wait
30-
@closed.wait
43+
if @reading
44+
@closed.wait
45+
else
46+
self.discard
47+
end
3148
end
3249

3350
def inspect

lib/async/http/client.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
require 'traces/provider'
1515

1616
require_relative 'protocol'
17+
require_relative 'body/finishable'
1718

1819
module Async
1920
module HTTP
@@ -140,7 +141,7 @@ def call(request)
140141
def inspect
141142
"#<#{self.class} authority=#{@authority.inspect}>"
142143
end
143-
144+
144145
Traces::Provider(self) do
145146
def call(request)
146147
attributes = {
@@ -186,9 +187,14 @@ def call(request)
186187
def make_response(request, connection)
187188
response = request.call(connection)
188189

190+
if body = request.body
191+
finishable = Body::Finishable.new(body)
192+
end
193+
189194
# The connection won't be released until the body is completely read/released.
190195
::Protocol::HTTP::Body::Completable.wrap(response) do
191196
# TODO: We should probably wait until the request is fully consumed and/or the connection is ready before releasing it back into the pool.
197+
finishable&.wait
192198

193199
# Release the connection back into the pool:
194200
@pool.release(connection)

lib/async/http/protocol/response.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def remote_address
3333
def remote_address= value
3434
@remote_address = value
3535
end
36+
37+
def finished!
38+
39+
end
3640
end
3741
end
3842
end

test/async/http/middleware/location_redirector.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
with '301' do
1919
let(:app) do
2020
Protocol::HTTP::Middleware.for do |request|
21-
request.finish # TODO: request.discard - or some default handling?
22-
2321
case request.path
2422
when '/home'
2523
Protocol::HTTP::Response[301, {'location' => '/'}, []]

0 commit comments

Comments
 (0)