Skip to content

Update to Faraday 1.0 #808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,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:

Expand Down
2 changes: 1 addition & 1 deletion elasticsearch-api/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace :test do
es_version_info = client.info['version']
build_hash = es_version_info['build_hash']
cluster_running = true
rescue Faraday::Error::ConnectionFailed
rescue Faraday::ConnectionFailed
STDERR.puts "[!] Test cluster not running?"
cluster_running = false
end
Expand Down
59 changes: 27 additions & 32 deletions elasticsearch-transport/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down Expand Up @@ -366,44 +370,35 @@ constructor, use the `transport_options` key:

To configure the _Faraday_ instance directly, use a block:

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

You can use any standard Faraday middleware and plugins in the configuration block,
for example sign the requests for the [AWS Elasticsearch service](https://aws.amazon.com/elasticsearch-service/):

require 'faraday_middleware/aws_signers_v4'

client = Elasticsearch::Client.new url: 'https://search-my-cluster-abc123....es.amazonaws.com' do |f|
f.request :aws_signers_v4,
credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY'], ENV['AWS_SECRET_ACCESS_KEY']),
service_name: 'es',
region: 'us-east-1'
end
You can use any standard Faraday middleware and plugins in the configuration block, for example sign the requests for the [AWS Elasticsearch service](https://aws.amazon.com/elasticsearch-service/). See [the AWS documentation](https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-request-signing.html#es-request-signing-ruby) for an example.

You can also initialize the transport class yourself, and pass it to the client constructor
as the `transport` argument:

require 'typhoeus'
require 'typhoeus/adapters/faraday'
```ruby
require 'patron'

transport_configuration = lambda do |f|
f.response :logger
f.adapter :patron
end

transport_configuration = lambda do |f|
f.response :logger
f.adapter :typhoeus
end
transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
hosts: [ { host: 'localhost', port: '9200' } ],
&transport_configuration

transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
hosts: [ { host: 'localhost', port: '9200' } ],
&transport_configuration
# Pass the transport to the client
#
client = Elasticsearch::Client.new transport: transport
```

# Pass the transport to the client
#
client = Elasticsearch::Client.new transport: transport

Instead of passing the transport to the constructor, you can inject it at run time:

Expand Down
2 changes: 1 addition & 1 deletion elasticsearch-transport/elasticsearch-transport.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = '>= 2.4'

s.add_dependency 'multi_json'
s.add_dependency 'faraday', '>= 0.14', '< 1'
s.add_dependency 'faraday', '~> 1'

s.add_development_dependency 'cane'
s.add_development_dependency 'curb' unless defined? JRUBY_VERSION
Expand Down
6 changes: 2 additions & 4 deletions elasticsearch-transport/lib/elasticsearch/transport/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __build_connection(host, options={}, block=nil)
# @return [Array]
#
def host_unreachable_exceptions
[::Faraday::Error::ConnectionFailed, ::Faraday::Error::TimeoutError]
[::Faraday::ConnectionFailed, ::Faraday::TimeoutError]
end

private
Expand Down
43 changes: 23 additions & 20 deletions elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -266,29 +266,33 @@
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

context 'when the Faraday adapter is configured' do

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
Expand Down Expand Up @@ -1209,23 +1213,22 @@
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
client.perform_request('GET', '_cluster/health')
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
Expand Down Expand Up @@ -1275,7 +1278,7 @@
expect(client.perform_request('GET', '_nodes/_local'))
expect {
client.perform_request('GET', '_nodes/_local')
}.to raise_exception(Faraday::Error::ConnectionFailed)
}.to raise_exception(Faraday::ConnectionFailed)
end
end

Expand Down Expand Up @@ -1559,12 +1562,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
Expand Down