new option: :max_requests

This is the max number of requests that can go on a connection. By
default, it'll be 100 for HTTP/1, and whatever the peer advertises as
MAX_CONCURRENT_STREAMS in HTTP/2 (usually 100).

If one would like to disable and reuse the socket beyond the advertised
number (sometimes the servers allow this), you can set this to
Float::INFINITY
This commit is contained in:
HoneyryderChuck 2020-03-14 23:30:54 +00:00
parent 89fde80f10
commit 70bf2a0e5a
4 changed files with 15 additions and 8 deletions

View File

@ -15,7 +15,7 @@ module HTTPX
def initialize(buffer, options)
@options = Options.new(options)
@max_concurrent_requests = @options.max_concurrent_requests || MAX_REQUESTS
@max_requests = MAX_REQUESTS
@max_requests = @options.max_requests || MAX_REQUESTS
@parser = Parser::HTTP1.new(self)
@buffer = buffer
@version = [1, 1]
@ -24,7 +24,7 @@ module HTTPX
end
def reset
@max_requests = @max_concurrent_requests
@max_requests = @options.max_requests || MAX_REQUESTS
@parser.reset!
end

View File

@ -8,7 +8,7 @@ module HTTPX
include Callbacks
include Loggable
MAX_CONCURRENT_REQUESTS = 200
MAX_CONCURRENT_REQUESTS = HTTP2Next::DEFAULT_MAX_CONCURRENT_STREAMS
Error = Class.new(Error) do
def initialize(id, code)
@ -21,7 +21,7 @@ module HTTPX
def initialize(buffer, options)
@options = Options.new(options)
@max_concurrent_requests = @options.max_concurrent_requests || MAX_CONCURRENT_REQUESTS
@max_requests = 0
@max_requests = @options.max_requests || 0
@pending = []
@streams = {}
@drains = {}
@ -107,6 +107,7 @@ module HTTPX
def init_connection
@connection = HTTP2Next::Client.new(@options.http2_settings)
@connection.max_streams = @max_requests if @connection.respond_to?(:max_streams=) && @max_requests.positive?
@connection.on(:frame, &method(:on_frame))
@connection.on(:frame_sent, &method(:on_frame_sent))
@connection.on(:frame_received, &method(:on_frame_received))
@ -221,7 +222,7 @@ module HTTPX
def on_settings(*)
@handshake_completed = true
@max_requests = @connection.remote_settings[:settings_max_concurrent_streams]
@max_requests = [@max_requests, @connection.remote_settings[:settings_max_concurrent_streams]].max
@max_concurrent_requests = [@max_concurrent_requests, @max_requests].min
send_pending

View File

@ -89,10 +89,15 @@ module HTTPX
end
def_option(:max_concurrent_requests) do |num|
max = Integer(num)
raise Error, ":max_concurrent_requests must be positive" unless max.positive?
raise Error, ":max_concurrent_requests must be positive" unless num.positive?
max
num
end
def_option(:max_requests) do |num|
raise Error, ":max_requests must be positive" unless num.positive?
num
end
def_option(:window_size) do |num|

View File

@ -70,6 +70,7 @@ class OptionsTest < Minitest::Test
:fallback_protocol => "http/1.1",
:headers => { "accept" => "xml", "foo" => "foo", "bar" => "bar" },
:max_concurrent_requests => nil,
:max_requests => nil,
:request_class => bar.request_class,
:response_class => bar.response_class,
:headers_class => bar.headers_class,