lib/httpx/connection/http1.rb in httpx-0.6.7 vs lib/httpx/connection/http1.rb in httpx-0.7.0
- old
+ new
@@ -5,34 +5,40 @@
module HTTPX
class Connection::HTTP1
include Callbacks
include Loggable
+ MAX_REQUESTS = 100
CRLF = "\r\n"
attr_reader :pending
def initialize(buffer, options)
@options = Options.new(options)
- @max_concurrent_requests = @options.max_concurrent_requests
- @max_requests = Float::INFINITY
+ @max_concurrent_requests = @options.max_concurrent_requests || MAX_REQUESTS
+ @max_requests = @options.max_requests || MAX_REQUESTS
@parser = Parser::HTTP1.new(self)
@buffer = buffer
@version = [1, 1]
@pending = []
@requests = []
end
def reset
+ @max_requests = @options.max_requests || MAX_REQUESTS
@parser.reset!
end
def close
reset
emit(:close)
end
+ def exhausted?
+ !@max_requests.positive?
+ end
+
def empty?
# this means that for every request there's an available
# partial response, so there are no in-flight requests waiting.
@requests.empty? || @requests.all? { |request| !request.response.nil? }
end
@@ -40,19 +46,20 @@
def <<(data)
@parser << data
end
def send(request)
- if @max_requests.positive? &&
- @requests.size >= @max_concurrent_requests
+ unless @max_requests.positive?
@pending << request
return
end
+
unless @requests.include?(request)
@requests << request
@pipelining = true if @requests.size > 1
end
+
handle(request)
end
def consume
@requests.each do |request|
@@ -161,16 +168,14 @@
if parameters.key?("timeout")
keep_alive_timeout = parameters["timeout"].to_i
emit(:timeout, keep_alive_timeout)
end
when /close/i
- @max_requests = Float::INFINITY
disable
when nil
# In HTTP/1.1, it's keep alive by default
return if response.version == "1.1"
- @max_requests = Float::INFINITY
disable
end
end
def disable