diff --git a/README.md b/README.md index d71c9fec3c..540c04088a 100644 --- a/README.md +++ b/README.md @@ -84,9 +84,7 @@ Both of these libraries are extensively documented. and the [`elasticsearch-api`](http://rubydoc.info/gems/elasticsearch-api) documentation carefully.** _Keep in mind, that for optimal performance, you should use a HTTP library which supports persistent -("keep-alive") connections, e.g. [Patron](https://github.com/toland/patron) or -[Typhoeus](https://github.com/typhoeus/typhoeus)._ These libraries are not dependencies of the elasticsearch gems, so -be sure to define a dependency in your own application. +("keep-alive") connections, e.g. [Patron](https://github.com/toland/patron)._ These libraries are not dependencies of the elasticsearch gems, so be sure to define a dependency in your own application. This repository contains these additional Ruby libraries: diff --git a/elasticsearch-transport/README.md b/elasticsearch-transport/README.md index 1670f886d5..c3cd71dabf 100644 --- a/elasticsearch-transport/README.md +++ b/elasticsearch-transport/README.md @@ -28,12 +28,16 @@ Features overview: * Node reloading (based on cluster state) on errors or on demand For optimal performance, use a HTTP library which supports persistent ("keep-alive") connections, -such as [Typhoeus](https://github.com/typhoeus/typhoeus). -Just require the library (`require 'typhoeus'; require 'typhoeus/adapters/faraday'`) in your code, -and it will be automatically used; currently these libraries will be automatically detected and used: -[Patron](https://github.com/toland/patron), -[HTTPClient](https://rubygems.org/gems/httpclient) and -[Net::HTTP::Persistent](https://rubygems.org/gems/net-http-persistent). +such as [patron](https://github.com/toland/patron). +Just require the library (`require 'patron'`) in your code, +and it will be automatically used. + +Currently these libraries will be automatically detected and used: +- [Patron](https://github.com/toland/patron) +- [HTTPClient](https://rubygems.org/gems/httpclient) +- [Net::HTTP::Persistent](https://rubygems.org/gems/net-http-persistent) + +**Note on [Typhoeus](https://github.com/typhoeus/typhoeus)**: Typhoeus is compatible and will be automatically detected too. However, the latest release (v1.3.1 at the moment of writing this) is not compatible with Faraday 1.0. [It still uses the deprecated `Faraday::Error` namespace](https://github.com/typhoeus/typhoeus/blob/v1.3.1/lib/typhoeus/adapters/faraday.rb#L100). If you want to use it with this gem, we suggest getting `master` from GitHub, since this has been fixed for v1.4.0. We'll update this if/when v1.4.0 is released.a For detailed information, see example configurations [below](#transport-implementations). @@ -436,12 +440,11 @@ client = Elasticsearch::Client.new transport_options: { To configure the _Faraday_ instance directly, use a block: ```ruby -require 'typhoeus' -require 'typhoeus/adapters/faraday' +require 'patron' client = Elasticsearch::Client.new(host: 'localhost', port: '9200') do |f| f.response :logger - f.adapter :typhoeus + f.adapter :patron end ``` @@ -451,12 +454,11 @@ You can also initialize the transport class yourself, and pass it to the client as the `transport` argument: ```ruby -require 'typhoeus' -require 'typhoeus/adapters/faraday' +require 'patron' transport_configuration = lambda do |f| f.response :logger - f.adapter :typhoeus + f.adapter :patron end transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \ diff --git a/elasticsearch-transport/elasticsearch-transport.gemspec b/elasticsearch-transport/elasticsearch-transport.gemspec index 3e2dad8783..2b611b06ad 100644 --- a/elasticsearch-transport/elasticsearch-transport.gemspec +++ b/elasticsearch-transport/elasticsearch-transport.gemspec @@ -19,50 +19,39 @@ Gem::Specification.new do |s| s.files = `git ls-files`.split($/) s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) } s.test_files = s.files.grep(%r{^(test|spec|features)/}) - s.require_paths = ["lib"] + s.require_paths = ['lib'] - s.extra_rdoc_files = [ "README.md", "LICENSE" ] - s.rdoc_options = [ "--charset=UTF-8" ] + s.extra_rdoc_files = [ 'README.md', 'LICENSE' ] + s.rdoc_options = [ '--charset=UTF-8' ] s.required_ruby_version = '>= 2.5' - s.add_dependency "multi_json" - s.add_dependency "faraday", '~>1' - - if defined?(RUBY_VERSION) && RUBY_VERSION < '1.9' - s.add_dependency "system_timer" - end - - s.add_development_dependency "bundler" - - s.add_development_dependency "rake" - - - s.add_development_dependency "ansi" - s.add_development_dependency "shoulda-context" - s.add_development_dependency "mocha" - s.add_development_dependency "yard" - s.add_development_dependency "pry" - - # Gems for testing integrations - s.add_development_dependency "curb" unless defined? JRUBY_VERSION - s.add_development_dependency "patron" unless defined? JRUBY_VERSION - s.add_development_dependency "typhoeus", '~> 0.6' - s.add_development_dependency "net-http-persistent" - s.add_development_dependency "httpclient" - s.add_development_dependency "manticore", '~> 0.6' if defined? JRUBY_VERSION - s.add_development_dependency "hashie" - - s.add_development_dependency "minitest" - s.add_development_dependency "minitest-reporters" - s.add_development_dependency "elasticsearch-extensions" - s.add_development_dependency "ruby-prof" unless defined?(JRUBY_VERSION) || defined?(Rubinius) - s.add_development_dependency "require-prof" unless defined?(JRUBY_VERSION) || defined?(Rubinius) - s.add_development_dependency "simplecov" - s.add_development_dependency "simplecov-rcov" - s.add_development_dependency "cane" - - s.add_development_dependency "test-unit", '~> 2' + s.add_dependency 'multi_json' + s.add_dependency 'faraday', '~> 1' + + s.add_development_dependency 'ansi' + s.add_development_dependency 'bundler' + s.add_development_dependency 'cane' + s.add_development_dependency 'curb' unless defined? JRUBY_VERSION + s.add_development_dependency 'elasticsearch-extensions' + s.add_development_dependency 'hashie' + s.add_development_dependency 'httpclient' + s.add_development_dependency 'manticore', '~> 0.6' if defined? JRUBY_VERSION + s.add_development_dependency 'minitest' + s.add_development_dependency 'minitest-reporters' + s.add_development_dependency 'mocha' + s.add_development_dependency 'net-http-persistent' + s.add_development_dependency 'patron' unless defined? JRUBY_VERSION + s.add_development_dependency 'pry' + s.add_development_dependency 'rake', '~> 11.1' + s.add_development_dependency 'require-prof' unless defined?(JRUBY_VERSION) || defined?(Rubinius) + s.add_development_dependency 'ruby-prof' unless defined?(JRUBY_VERSION) || defined?(Rubinius) + s.add_development_dependency 'shoulda-context' + s.add_development_dependency 'simplecov', '~> 0.17', '< 0.18' + s.add_development_dependency 'simplecov-rcov' + s.add_development_dependency 'test-unit', '~> 2' + s.add_development_dependency 'typhoeus', '~> 0.6' + s.add_development_dependency 'yard' s.description = <<-DESC.gsub(/^ /, '') Ruby client for Elasticsearch. See the `elasticsearch` gem for full integration. diff --git a/elasticsearch-transport/lib/elasticsearch/transport/client.rb b/elasticsearch-transport/lib/elasticsearch/transport/client.rb index e2cef39466..575570b94f 100644 --- a/elasticsearch-transport/lib/elasticsearch/transport/client.rb +++ b/elasticsearch-transport/lib/elasticsearch/transport/client.rb @@ -147,10 +147,8 @@ def initialize(arguments={}, &block) transport_class = @arguments[:transport_class] || DEFAULT_TRANSPORT_CLASS if transport_class == Transport::HTTP::Faraday @transport = transport_class.new(hosts: @seeds, options: @arguments) do |faraday| - block.call faraday if block - unless (h = faraday.builder.handlers.last) && h.name.start_with?("Faraday::Adapter") - faraday.adapter(@arguments[:adapter] || __auto_detect_adapter) - end + faraday.adapter(@arguments[:adapter] || __auto_detect_adapter) + block&.call faraday end else @transport = transport_class.new(hosts: @seeds, options: @arguments) diff --git a/elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb b/elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb index 740fc75569..26ebf80c9e 100644 --- a/elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb +++ b/elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb @@ -220,41 +220,41 @@ describe 'adapter' do context 'when no adapter is specified' do let(:adapter) do - client.transport.connections.all.first.connection.builder.handlers + client.transport.connections.all.first.connection.builder.adapter end it 'uses Faraday NetHttp' do - expect(adapter).to include(Faraday::Adapter::NetHttp) + expect(adapter).to eq Faraday::Adapter::NetHttp end end context 'when the adapter is specified' do let(:adapter) do - client.transport.connections.all.first.connection.builder.handlers + client.transport.connections.all.first.connection.builder.adapter end let(:client) do - described_class.new(adapter: :typhoeus) + described_class.new(adapter: :patron) end it 'uses Faraday with the adapter' do - expect(adapter).to include(Faraday::Adapter::Typhoeus) + expect(adapter).to eq Faraday::Adapter::Patron end end context 'when the adapter is specified as a string key' do let(:adapter) do - client.transport.connections.all.first.connection.builder.handlers + client.transport.connections.all.first.connection.builder.adapter end let(:client) do - described_class.new('adapter' => :typhoeus) + described_class.new('adapter' => :patron) end it 'uses Faraday with the adapter' do - expect(adapter).to include(Faraday::Adapter::Typhoeus) + expect(adapter).to eq Faraday::Adapter::Patron end end @@ -266,11 +266,11 @@ end let(:adapter) do - client.transport.connections.all.first.connection.builder.handlers + client.transport.connections.all.first.connection.builder.adapter end it 'uses the detected adapter' do - expect(adapter).to include(Faraday::Adapter::Patron) + expect(adapter).to eq Faraday::Adapter::Patron end end @@ -278,17 +278,21 @@ let(:client) do described_class.new do |faraday| - faraday.adapter :typhoeus + faraday.adapter :patron faraday.response :logger end end + let(:adapter) do + client.transport.connections.all.first.connection.builder.adapter + end + let(:handlers) do client.transport.connections.all.first.connection.builder.handlers end it 'sets the adapter' do - expect(handlers).to include(Faraday::Adapter::Typhoeus) + expect(adapter).to eq Faraday::Adapter::Patron end it 'sets the logger' do @@ -1395,15 +1399,14 @@ end context 'when the Faraday adapter is set in the block' do - let(:client) do Elasticsearch::Client.new(host: ELASTICSEARCH_HOSTS.first, logger: logger) do |client| client.adapter(:net_http_persistent) end end - let(:connection_handler) do - client.transport.connections.first.connection.builder.handlers.first + let(:handler_name) do + client.transport.connections.first.connection.builder.adapter.name end let(:response) do @@ -1411,7 +1414,7 @@ end it 'sets the adapter' do - expect(connection_handler.name).to eq('Faraday::Adapter::NetHttpPersistent') + expect(handler_name).to eq('Faraday::Adapter::NetHttpPersistent') end it 'uses the adapter to connect' do @@ -1745,12 +1748,12 @@ { adapter: :patron } end - let(:connection_handler) do - client.transport.connections.first.connection.builder.handlers.first + let(:adapter) do + client.transport.connections.first.connection.builder.adapter end it 'uses the patron connection handler' do - expect(connection_handler).to eq('Faraday::Adapter::Patron') + expect(adapter).to eq('Faraday::Adapter::Patron') end it 'keeps connections open' do