lib/httpx/connection/http1.rb in httpx-0.7.0 vs lib/httpx/connection/http1.rb in httpx-0.8.0
- old
+ new
@@ -21,18 +21,31 @@
@version = [1, 1]
@pending = []
@requests = []
end
+ def interests
+ # this means we're processing incoming response already
+ return :r if @request
+
+ return if @requests.empty?
+
+ request = @requests.first
+
+ return :w if request.interests == :w || !@buffer.empty?
+
+ :r
+ end
+
def reset
@max_requests = @options.max_requests || MAX_REQUESTS
@parser.reset!
end
def close
reset
- emit(:close)
+ emit(:close, true)
end
def exhausted?
!@max_requests.positive?
end
@@ -51,20 +64,22 @@
unless @max_requests.positive?
@pending << request
return
end
- unless @requests.include?(request)
- @requests << request
- @pipelining = true if @requests.size > 1
- end
+ return if @requests.include?(request)
- handle(request)
+ @requests << request
+ @pipelining = true if @requests.size > 1
end
def consume
- @requests.each do |request|
+ requests_limit = [@max_concurrent_requests, @max_requests, @requests.size].min
+ @requests.each_with_index do |request, idx|
+ break if idx >= requests_limit
+ next if request.state == :done
+
handle(request)
end
end
# HTTP Parser callbacks
@@ -119,11 +134,11 @@
dispatch
end
def dispatch
if @request.expects?
- reset
+ @parser.reset!
return handle(@request)
end
request = @request
@request = nil
@@ -134,20 +149,23 @@
if @parser.upgrade?
response << @parser.upgrade_data
throw(:called)
end
- reset
+ @parser.reset!
@max_requests -= 1
- send(@pending.shift) unless @pending.empty?
manage_connection(response)
+ send(@pending.shift) unless @pending.empty?
end
def handle_error(ex)
if @pipelining
disable
else
@requests.each do |request|
+ emit(:error, request, ex)
+ end
+ @pending.each do |request|
emit(:error, request, ex)
end
end
end