mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-04 00:00:37 -04:00
persistent and retries plugin now work well together
When persistent and retries are loaded separately, the options won't be just overwritten; instead, changes will be kept, and max_retries will be the max value of what both plugins load (in case the user sets its own option)
This commit is contained in:
parent
01552757a0
commit
62467ff5c5
@ -19,7 +19,12 @@ module HTTPX
|
||||
#
|
||||
module Persistent
|
||||
def self.load_dependencies(klass)
|
||||
klass.plugin(:retries, max_retries: 1, retry_change_requests: true)
|
||||
max_retries = if klass.default_options.respond_to?(:max_retries)
|
||||
[klass.default_options.max_retries, 1].max
|
||||
else
|
||||
1
|
||||
end
|
||||
klass.plugin(:retries, max_retries: max_retries, retry_change_requests: true)
|
||||
end
|
||||
|
||||
def self.extra_options(options)
|
||||
|
@ -17,6 +17,7 @@ module HTTPX
|
||||
|
||||
def load_dependencies(klass)
|
||||
klass.plugin(:retries,
|
||||
retry_change_requests: true,
|
||||
retry_on: method(:retry_on_rate_limited_response),
|
||||
retry_after: method(:retry_after_rate_limit))
|
||||
end
|
||||
|
@ -221,7 +221,7 @@ module HTTPX
|
||||
def plugin(pl, options = nil, &block)
|
||||
# raise Error, "Cannot add a plugin to a frozen config" if frozen?
|
||||
pl = Plugins.load_plugin(pl) if pl.is_a?(Symbol)
|
||||
unless @plugins.include?(pl)
|
||||
if !@plugins.include?(pl)
|
||||
@plugins << pl
|
||||
pl.load_dependencies(self, &block) if pl.respond_to?(:load_dependencies)
|
||||
@default_options = @default_options.dup
|
||||
@ -245,6 +245,13 @@ module HTTPX
|
||||
opts.connection_class.__send__(:include, pl::ConnectionMethods) if defined?(pl::ConnectionMethods)
|
||||
pl.configure(self, &block) if pl.respond_to?(:configure)
|
||||
|
||||
@default_options.freeze
|
||||
elsif options
|
||||
# this can happen when two plugins are loaded, an one of them calls the other under the hood,
|
||||
# albeit changing some default.
|
||||
@default_options = @default_options.dup
|
||||
@default_options = @default_options.merge(options)
|
||||
|
||||
@default_options.freeze
|
||||
end
|
||||
self
|
||||
|
@ -26,6 +26,7 @@ class HTTPSTest < Minitest::Test
|
||||
include Plugins::Multipart
|
||||
include Plugins::Expect
|
||||
include Plugins::RateLimiter
|
||||
include Plugins::Persistent unless RUBY_ENGINE == "jruby" || RUBY_VERSION < "2.3"
|
||||
|
||||
def test_connection_coalescing
|
||||
coalesced_origin = "https://#{ENV["HTTPBIN_COALESCING_HOST"]}"
|
||||
|
38
test/support/requests/plugins/persistent.rb
Normal file
38
test/support/requests/plugins/persistent.rb
Normal file
@ -0,0 +1,38 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Requests
|
||||
module Plugins
|
||||
module Persistent
|
||||
def test_persistent
|
||||
uri = build_uri("/get")
|
||||
|
||||
non_persistent_session = HTTPX.plugin(SessionWithPool)
|
||||
response = non_persistent_session.get(uri)
|
||||
verify_status(response, 200)
|
||||
assert non_persistent_session.pool.connections.empty?, "unexpected connections ()"
|
||||
|
||||
persistent_session = non_persistent_session.plugin(:persistent)
|
||||
response = persistent_session.get(uri)
|
||||
verify_status(response, 200)
|
||||
assert persistent_session.pool.connections.size == 1, "unexpected connections ()"
|
||||
|
||||
persistent_session.close
|
||||
assert persistent_session.pool.connections.empty?, "unexpected connections ()"
|
||||
end
|
||||
|
||||
def test_persistent_options
|
||||
retry_persistent_session = HTTPX.plugin(:persistent).plugin(:retries, max_retries: 4)
|
||||
options = retry_persistent_session.send(:default_options)
|
||||
assert options.max_retries == 4
|
||||
assert options.retry_change_requests
|
||||
assert options.persistent
|
||||
|
||||
persistent_retry_session = HTTPX.plugin(:retries, max_retries: 4).plugin(:persistent)
|
||||
options = persistent_retry_session.send(:default_options)
|
||||
assert options.max_retries == 4
|
||||
assert options.retry_change_requests
|
||||
assert options.persistent
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user