From 14d0dd70c4c14bedaed70d9e72365a93ca4c429d Mon Sep 17 00:00:00 2001 From: Mike Rogers Date: Fri, 26 Mar 2021 08:00:05 +0000 Subject: [PATCH] Replacing Net::HTTP::Persistent with faraday-net_http_persistent (#1250) --- Gemfile | 3 - faraday.gemspec | 1 + lib/faraday/adapter/net_http_persistent.rb | 91 ------------------- .../adapter/net_http_persistent_spec.rb | 57 ------------ 4 files changed, 1 insertion(+), 151 deletions(-) delete mode 100644 lib/faraday/adapter/net_http_persistent.rb delete mode 100644 spec/faraday/adapter/net_http_persistent_spec.rb diff --git a/Gemfile b/Gemfile index c6d8ce750..7b2143c6d 100644 --- a/Gemfile +++ b/Gemfile @@ -24,9 +24,6 @@ group :test, :development do gem 'excon', '>= 0.27.4' gem 'httpclient', '>= 2.2' gem 'multipart-parser' - # TODO: remove this once v4 is released - options = (RUBY_VERSION.start_with?('3') ? { github: 'grosser/net-http-persistent', branch: 'grosser/spec' } : {}) - gem 'net-http-persistent', '>= 3.0', **options gem 'patron', '>= 0.4.2', platforms: :ruby gem 'rack-test', '>= 0.6', require: 'rack/test' gem 'rspec', '~> 3.7' diff --git a/faraday.gemspec b/faraday.gemspec index 96e0669f4..12e6bd137 100644 --- a/faraday.gemspec +++ b/faraday.gemspec @@ -16,6 +16,7 @@ Gem::Specification.new do |spec| spec.required_ruby_version = '>= 2.4' spec.add_dependency 'faraday-net_http', '~> 1.0' + spec.add_dependency 'faraday-net_http_persistent', '~> 1.0' spec.add_dependency 'multipart-post', '>= 1.2', '< 3' spec.add_dependency 'ruby2_keywords', '>= 0.0.4' diff --git a/lib/faraday/adapter/net_http_persistent.rb b/lib/faraday/adapter/net_http_persistent.rb deleted file mode 100644 index 26bcc2ce5..000000000 --- a/lib/faraday/adapter/net_http_persistent.rb +++ /dev/null @@ -1,91 +0,0 @@ -# frozen_string_literal: true - -module Faraday - class Adapter - # Net::HTTP::Persistent adapter. - class NetHttpPersistent < NetHttp - dependency 'net/http/persistent' - - private - - def net_http_connection(env) - @cached_connection ||= - if Net::HTTP::Persistent.instance_method(:initialize) - .parameters.first == %i[key name] - options = { name: 'Faraday' } - if @connection_options.key?(:pool_size) - options[:pool_size] = @connection_options[:pool_size] - end - Net::HTTP::Persistent.new(**options) - else - Net::HTTP::Persistent.new('Faraday') - end - - proxy_uri = proxy_uri(env) - if @cached_connection.proxy_uri != proxy_uri - @cached_connection.proxy = proxy_uri - end - @cached_connection - end - - def proxy_uri(env) - proxy_uri = nil - if (proxy = env[:request][:proxy]) - proxy_uri = if proxy[:uri].is_a?(::URI::HTTP) - proxy[:uri].dup - else - ::URI.parse(proxy[:uri].to_s) - end - proxy_uri.user = proxy_uri.password = nil - # awful patch for net-http-persistent 2.8 - # not unescaping user/password - if proxy[:user] - (class << proxy_uri; self; end).class_eval do - define_method(:user) { proxy[:user] } - define_method(:password) { proxy[:password] } - end - end - end - proxy_uri - end - - def perform_request(http, env) - http.request env[:url], create_request(env) - rescue Errno::ETIMEDOUT, Net::OpenTimeout => e - raise Faraday::TimeoutError, e - rescue Net::HTTP::Persistent::Error => e - raise Faraday::TimeoutError, e if e.message.include? 'Timeout' - - if e.message.include? 'connection refused' - raise Faraday::ConnectionFailed, e - end - - raise - end - - SSL_CONFIGURATIONS = { - certificate: :client_cert, - private_key: :client_key, - ca_file: :ca_file, - ssl_version: :version, - min_version: :min_version, - max_version: :max_version - }.freeze - - def configure_ssl(http, ssl) - return unless ssl - - http_set(http, :verify_mode, ssl_verify_mode(ssl)) - http_set(http, :cert_store, ssl_cert_store(ssl)) - - SSL_CONFIGURATIONS - .select { |_, key| ssl[key] } - .each { |target, key| http_set(http, target, ssl[key]) } - end - - def http_set(http, attr, value) - http.send("#{attr}=", value) if http.send(attr) != value - end - end - end -end diff --git a/spec/faraday/adapter/net_http_persistent_spec.rb b/spec/faraday/adapter/net_http_persistent_spec.rb deleted file mode 100644 index 3a35e0a56..000000000 --- a/spec/faraday/adapter/net_http_persistent_spec.rb +++ /dev/null @@ -1,57 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe Faraday::Adapter::NetHttpPersistent do - features :request_body_on_query_methods, :reason_phrase_parse, :compression, :trace_method - - it_behaves_like 'an adapter' - - it 'allows to provide adapter specific configs' do - url = URI('https://example.com') - - adapter = described_class.new do |http| - http.idle_timeout = 123 - end - - http = adapter.send(:connection, url: url, request: {}) - adapter.send(:configure_request, http, {}) - - expect(http.idle_timeout).to eq(123) - end - - it 'sets max_retries to 0' do - url = URI('http://example.com') - - adapter = described_class.new - - http = adapter.send(:connection, url: url, request: {}) - adapter.send(:configure_request, http, {}) - - # `max_retries=` is only present in Ruby 2.5 - expect(http.max_retries).to eq(0) if http.respond_to?(:max_retries=) - end - - it 'allows to set pool_size on initialize' do - url = URI('https://example.com') - - adapter = described_class.new(nil, pool_size: 5) - - http = adapter.send(:connection, url: url, request: {}) - - # `pool` is only present in net_http_persistent >= 3.0 - expect(http.pool.size).to eq(5) if http.respond_to?(:pool) - end - - context 'min_version' do - it 'allows to set min_version in SSL settings' do - url = URI('https://example.com') - - adapter = described_class.new(nil) - - http = adapter.send(:connection, url: url, request: {}) - adapter.send(:configure_ssl, http, min_version: :TLS1_2) - - # `min_version` is only present in net_http_persistent >= 3.1 (UNRELEASED) - expect(http.min_version).to eq(:TLS1_2) if http.respond_to?(:min_version) - end - end -end