Skip to content

Commit f7b0795

Browse files
committed
Rename keep_alive to persistent. Add a request/response count.
1 parent 3ee731f commit f7b0795

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

lib/async/http/protocol/http10.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def version
3131
VERSION
3232
end
3333

34-
def keep_alive?(headers)
34+
def persistent?(headers)
3535
headers['connection'] == KEEP_ALIVE
3636
end
3737

@@ -44,7 +44,7 @@ def receive_requests
4444

4545
write_response(response.version, response.status, response.headers, response.body)
4646

47-
unless keep_alive?(request.headers) && keep_alive?(headers)
47+
unless persistent?(request.headers) && persistent?(headers)
4848
@keep_alive = false
4949

5050
break
@@ -54,9 +54,9 @@ def receive_requests
5454
return false
5555
end
5656

57-
def write_body(body, chunked = true)
57+
def write_body(body, chunked = false)
5858
# We don't support chunked encoding.
59-
super(body, false)
59+
super(body, chunked)
6060
end
6161

6262
def read_body(headers)
@@ -65,7 +65,7 @@ def read_body(headers)
6565
end
6666

6767
# Technically, with HTTP/1.0, if no content-length is specified, we just need to read everything until the connection is closed.
68-
if !keep_alive?(headers)
68+
if !persistent?(headers)
6969
return Body::Remainder.new(@stream)
7070
end
7171
end

lib/async/http/protocol/http11.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,19 @@ class HTTP11 < Async::IO::Protocol::Line
3636
def initialize(stream)
3737
super(stream, CRLF)
3838

39-
@keep_alive = true
39+
@persistent = true
40+
@count = 0
4041
end
4142

43+
attr :count
44+
4245
# Only one simultaneous connection at a time.
4346
def multiplex
4447
1
4548
end
4649

4750
def reusable?
48-
@keep_alive
51+
@persistent
4952
end
5053

5154
class << self
@@ -62,14 +65,15 @@ def version
6265
VERSION
6366
end
6467

65-
def keep_alive?(headers)
68+
def persistent?(headers)
6669
headers['connection'] != CLOSE
6770
end
6871

6972
# Server loop.
7073
def receive_requests(task: Task.current)
7174
while true
7275
request = Request.new(*read_request)
76+
@count += 1
7377

7478
response = yield request
7579

@@ -79,8 +83,8 @@ def receive_requests(task: Task.current)
7983

8084
request.finish
8185

82-
unless keep_alive?(request.headers) and keep_alive?(response.headers)
83-
@keep_alive = false
86+
unless persistent?(request.headers) and persistent?(response.headers)
87+
@persistent = false
8488

8589
break
8690
end
@@ -91,13 +95,16 @@ def receive_requests(task: Task.current)
9195
end
9296

9397
def call(request)
98+
@count += 1
99+
94100
request.version ||= self.version
95101

96102
Async.logger.debug(self) {"#{request.method} #{request.path} #{request.headers.inspect}"}
97103
write_request(request.authority, request.method, request.path, request.version, request.headers, request.body)
98104

99105
return Response.new(*read_response)
100106
rescue EOFError
107+
Async.logger.debug(self) {"Connection failed with EOFError after #{@count} requests."}
101108
return nil
102109
end
103110

@@ -118,7 +125,7 @@ def read_response
118125
headers = read_headers
119126
body = read_body(headers)
120127

121-
@keep_alive = keep_alive?(headers)
128+
@keep_alive = persistent?(headers)
122129

123130
return version, Integer(status), reason, headers, body
124131
end
@@ -163,7 +170,7 @@ def read_headers(headers = {})
163170
end
164171

165172
def write_body(body, chunked = true)
166-
if body.empty?
173+
if body.nil? or body.empty?
167174
@stream.write("Content-Length: 0\r\n\r\n")
168175
body.read
169176
elsif chunked

lib/async/http/protocol/http2.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ def initialize(controller, stream)
6464
@controller.on(:frame_received) do |frame|
6565
Async.logger.debug(self) {"Received frame: #{frame.inspect}"}
6666
end
67+
68+
@count = 0
6769
end
6870

71+
attr :count
72+
6973
# Multiple requests can be processed at the same time.
7074
def multiplex
7175
@controller.remote_settings[:settings_max_concurrent_streams]
@@ -106,6 +110,8 @@ def close
106110
def receive_requests(task: Task.current, &block)
107111
# emits new streams opened by the client
108112
@controller.on(:stream) do |stream|
113+
@count += 1
114+
109115
request = Request.new
110116
request.version = self.version
111117
request.headers = {}
@@ -145,7 +151,7 @@ def receive_requests(task: Task.current, &block)
145151
headers.update(response.headers)
146152

147153
# puts "Sending headers #{headers}"
148-
if response.body.empty?
154+
if response.body.nil? or response.body.empty?
149155
stream.headers(headers, end_stream: true)
150156
response.body.read
151157
else
@@ -168,6 +174,8 @@ def receive_requests(task: Task.current, &block)
168174
end
169175

170176
def call(request)
177+
@count += 1
178+
171179
request.version ||= self.version
172180

173181
stream = @controller.new_stream

0 commit comments

Comments
 (0)