elasticsearch-transport 7.1.0 → 7.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -16
  3. data/{LICENSE.txt → LICENSE} +0 -0
  4. data/README.md +18 -19
  5. data/Rakefile +3 -16
  6. data/elasticsearch-transport.gemspec +7 -19
  7. data/lib/elasticsearch/transport/client.rb +27 -24
  8. data/lib/elasticsearch/transport/redacted.rb +3 -16
  9. data/lib/elasticsearch/transport/transport/base.rb +81 -26
  10. data/lib/elasticsearch/transport/transport/connections/collection.rb +3 -16
  11. data/lib/elasticsearch/transport/transport/connections/connection.rb +3 -16
  12. data/lib/elasticsearch/transport/transport/connections/selector.rb +20 -21
  13. data/lib/elasticsearch/transport/transport/errors.rb +3 -16
  14. data/lib/elasticsearch/transport/transport/http/curb.rb +28 -24
  15. data/lib/elasticsearch/transport/transport/http/faraday.rb +19 -18
  16. data/lib/elasticsearch/transport/transport/http/manticore.rb +27 -25
  17. data/lib/elasticsearch/transport/transport/loggable.rb +3 -16
  18. data/lib/elasticsearch/transport/transport/response.rb +3 -16
  19. data/lib/elasticsearch/transport/transport/serializer/multi_json.rb +3 -16
  20. data/lib/elasticsearch/transport/transport/sniffer.rb +5 -17
  21. data/lib/elasticsearch/transport/version.rb +4 -17
  22. data/lib/elasticsearch/transport.rb +3 -16
  23. data/lib/elasticsearch-transport.rb +3 -16
  24. data/spec/elasticsearch/connections/collection_spec.rb +241 -0
  25. data/spec/elasticsearch/connections/selector_spec.rb +161 -0
  26. data/spec/elasticsearch/transport/base_spec.rb +186 -24
  27. data/spec/elasticsearch/transport/client_spec.rb +350 -19
  28. data/spec/elasticsearch/transport/sniffer_spec.rb +3 -16
  29. data/spec/spec_helper.rb +6 -0
  30. data/test/integration/transport_test.rb +3 -16
  31. data/test/profile/client_benchmark_test.rb +3 -16
  32. data/test/test_helper.rb +3 -16
  33. data/test/unit/connection_test.rb +3 -16
  34. data/test/unit/response_test.rb +3 -16
  35. data/test/unit/serializer_test.rb +3 -16
  36. data/test/unit/transport_base_test.rb +3 -16
  37. data/test/unit/transport_curb_test.rb +4 -17
  38. data/test/unit/transport_faraday_test.rb +3 -16
  39. data/test/unit/transport_manticore_test.rb +30 -27
  40. metadata +42 -16
  41. data/test/unit/connection_collection_test.rb +0 -147
  42. data/test/unit/connection_selector_test.rb +0 -81
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 14188a454b64314c13f7c9279741fc8df5f9f03ff9813f45aeca96abc8da7e39
4
- data.tar.gz: dfcd7ac5fb8e0b2a0450b2939272f88a329581648c47b130d62e1fcead705b44
3
+ metadata.gz: 271a43455912981268da637666dddf8d9201752ba7b88787284dfc79039ac0e7
4
+ data.tar.gz: 92f3307366179b9eb868b2224e94b87570ffee878a9013d9419f10271731aa6e
5
5
  SHA512:
6
- metadata.gz: 36e9583f541ec75994cb27479c16f4624d7ea6a9e6f33360d7cac69541fe10a5c2ee2d7f20b2c1231b5fd07ab85cd32baefb236d6ab77ff11635bb23b1d9d801
7
- data.tar.gz: 95faf1016519c04dcafcd6580356dcfb00c8eeb9ef3301bf2ff3c66a90cfcc8ebf676b638ec18f92f708acb5ef01ee2f7ee137f23ed6532774b371e47b02e11f
6
+ metadata.gz: 1ead63661251f6b672a28f3bd759b86049f159225bebf2c053fbaaf5a03d3d974082131595980a2e44ef928fdcc640ce85e271398d5e3ac13cd0daaf5daf84a9
7
+ data.tar.gz: 9e99b4a07e919000b06864741380293eb073b0a3ad71e227edf3a9a443b12a4ca26cbe573d33429748561059bec51b4a315345e52d15538e6dab0d84230b8aa2
data/Gemfile CHANGED
@@ -1,19 +1,6 @@
1
- # Licensed to Elasticsearch B.V. under one or more contributor
2
- # license agreements. See the NOTICE file distributed with
3
- # this work for additional information regarding copyright
4
- # ownership. Elasticsearch B.V. licenses this file to you under
5
- # the Apache License, Version 2.0 (the "License"); you may
6
- # not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing,
12
- # software distributed under the License is distributed on an
13
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- # KIND, either express or implied. See the License for the
15
- # specific language governing permissions and limitations
16
- # under the License.
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
17
4
 
18
5
  source 'https://rubygems.org'
19
6
 
File without changes
data/README.md CHANGED
@@ -106,6 +106,22 @@ Another way to configure the URL(s) is to export the `ELASTICSEARCH_URL` variabl
106
106
  The client will automatically round-robin across the hosts
107
107
  (unless you select or implement a different [connection selector](#connection-selector)).
108
108
 
109
+ ### Default port
110
+
111
+ The default port is `9200`. Please specify a port for your host(s) if they differ from this default.
112
+ Please see below for an exception to this when connecting using an Elastic Cloud ID.
113
+
114
+ ### Connect using an Elastic Cloud ID
115
+
116
+ If you are using [Elastic Cloud](https://www.elastic.co/cloud), you can provide your cloud id to the client.
117
+ You must supply your username and password separately, and optionally a port. If no port is supplied,
118
+ port 9243 will be used.
119
+
120
+ Note: Do not enable sniffing when using Elastic Cloud. The nodes are behind a load balancer so
121
+ Elastic Cloud will take care of everything for you.
122
+
123
+ Elasticsearch::Client.new(cloud_id: 'name:bG9jYWxob3N0JGFiY2QkZWZnaA==', user: 'elastic', password: 'changeme')
124
+
109
125
  ### Authentication
110
126
 
111
127
  You can pass the authentication credentials, scheme and port in the host configuration hash:
@@ -144,7 +160,7 @@ You can customize the default logger or tracer:
144
160
  client.transport.logger.formatter = proc { |s, d, p, m| "#{s}: #{m}\n" }
145
161
  client.transport.logger.level = Logger::INFO
146
162
 
147
- Or, you can use a custom {::Logger} instance:
163
+ Or, you can use a custom `::Logger` instance:
148
164
 
149
165
  Elasticsearch::Client.new logger: Logger.new(STDERR)
150
166
 
@@ -458,21 +474,4 @@ can use Ruby 2.x syntax and features.
458
474
 
459
475
  ## License
460
476
 
461
- This software is licensed under the Apache 2 license, quoted below.
462
-
463
- Licensed to Elasticsearch B.V. under one or more contributor
464
- license agreements. See the NOTICE file distributed with
465
- this work for additional information regarding copyright
466
- ownership. Elasticsearch B.V. licenses this file to you under
467
- the Apache License, Version 2.0 (the "License"); you may
468
- not use this file except in compliance with the License.
469
- You may obtain a copy of the License at
470
-
471
- http://www.apache.org/licenses/LICENSE-2.0
472
-
473
- Unless required by applicable law or agreed to in writing,
474
- software distributed under the License is distributed on an
475
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
476
- KIND, either express or implied. See the License for the
477
- specific language governing permissions and limitations
478
- under the License.
477
+ This software is licensed under the [Apache 2 license](./LICENSE).
data/Rakefile CHANGED
@@ -1,19 +1,6 @@
1
- # Licensed to Elasticsearch B.V. under one or more contributor
2
- # license agreements. See the NOTICE file distributed with
3
- # this work for additional information regarding copyright
4
- # ownership. Elasticsearch B.V. licenses this file to you under
5
- # the Apache License, Version 2.0 (the "License"); you may
6
- # not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing,
12
- # software distributed under the License is distributed on an
13
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- # KIND, either express or implied. See the License for the
15
- # specific language governing permissions and limitations
16
- # under the License.
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
17
4
 
18
5
  require "bundler/gem_tasks"
19
6
 
@@ -1,19 +1,6 @@
1
- # Licensed to Elasticsearch B.V. under one or more contributor
2
- # license agreements. See the NOTICE file distributed with
3
- # this work for additional information regarding copyright
4
- # ownership. Elasticsearch B.V. licenses this file to you under
5
- # the Apache License, Version 2.0 (the "License"); you may
6
- # not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing,
12
- # software distributed under the License is distributed on an
13
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- # KIND, either express or implied. See the License for the
15
- # specific language governing permissions and limitations
16
- # under the License.
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
17
4
 
18
5
  # coding: utf-8
19
6
  lib = File.expand_path('../lib', __FILE__)
@@ -34,13 +21,13 @@ Gem::Specification.new do |s|
34
21
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
35
22
  s.require_paths = ["lib"]
36
23
 
37
- s.extra_rdoc_files = [ "README.md", "LICENSE.txt" ]
24
+ s.extra_rdoc_files = [ "README.md", "LICENSE" ]
38
25
  s.rdoc_options = [ "--charset=UTF-8" ]
39
26
 
40
27
  s.required_ruby_version = '>= 1.9'
41
28
 
42
29
  s.add_dependency "multi_json"
43
- s.add_dependency "faraday"
30
+ s.add_dependency "faraday", '>= 0.14', "< 1"
44
31
 
45
32
  if defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
46
33
  s.add_dependency "system_timer"
@@ -65,6 +52,7 @@ Gem::Specification.new do |s|
65
52
  s.add_development_dependency "patron" unless defined? JRUBY_VERSION
66
53
  s.add_development_dependency "typhoeus", '~> 0.6'
67
54
  s.add_development_dependency "net-http-persistent"
55
+ s.add_development_dependency "httpclient"
68
56
  s.add_development_dependency "manticore", '~> 0.6' if defined? JRUBY_VERSION
69
57
  s.add_development_dependency "hashie"
70
58
 
@@ -80,7 +68,7 @@ Gem::Specification.new do |s|
80
68
  s.add_development_dependency "elasticsearch-extensions"
81
69
  s.add_development_dependency "ruby-prof" unless defined?(JRUBY_VERSION) || defined?(Rubinius)
82
70
  s.add_development_dependency "require-prof" unless defined?(JRUBY_VERSION) || defined?(Rubinius)
83
- s.add_development_dependency "simplecov"
71
+ s.add_development_dependency "simplecov", '~> 0.17', '< 0.18'
84
72
  s.add_development_dependency "simplecov-rcov"
85
73
  s.add_development_dependency "cane"
86
74
  end
@@ -1,19 +1,8 @@
1
- # Licensed to Elasticsearch B.V. under one or more contributor
2
- # license agreements. See the NOTICE file distributed with
3
- # this work for additional information regarding copyright
4
- # ownership. Elasticsearch B.V. licenses this file to you under
5
- # the Apache License, Version 2.0 (the "License"); you may
6
- # not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing,
12
- # software distributed under the License is distributed on an
13
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- # KIND, either express or implied. See the License for the
15
- # specific language governing permissions and limitations
16
- # under the License.
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
4
+
5
+ require 'base64'
17
6
 
18
7
  module Elasticsearch
19
8
  module Transport
@@ -46,6 +35,11 @@ module Elasticsearch
46
35
  # @since 7.0.0
47
36
  DEFAULT_HOST = 'localhost:9200'.freeze
48
37
 
38
+ # The default port to use if connecting using a Cloud ID.
39
+ #
40
+ # @since 7.2.0
41
+ DEFAULT_CLOUD_PORT = 9243
42
+
49
43
  # Returns the transport object.
50
44
  #
51
45
  # @see Elasticsearch::Transport::Transport::Base
@@ -101,6 +95,9 @@ module Elasticsearch
101
95
  #
102
96
  # @option arguments [String] :send_get_body_as Specify the HTTP method to use for GET requests with a body.
103
97
  # (Default: GET)
98
+ # @option arguments [true, false] :compression Whether to compress requests. Gzip compression will be used.
99
+ # The default is false. Responses will automatically be inflated if they are compressed.
100
+ # If a custom transport object is used, it must handle the request compression and response inflation.
104
101
  #
105
102
  # @yield [faraday] Access and configure the `Faraday::Connection` instance directly with a block
106
103
  #
@@ -115,9 +112,10 @@ module Elasticsearch
115
112
  @arguments[:randomize_hosts] ||= false
116
113
  @arguments[:transport_options] ||= {}
117
114
  @arguments[:http] ||= {}
118
- @options[:http] ||= {}
115
+ @options[:http] ||= {}
119
116
 
120
- @seeds = __extract_hosts(@arguments[:hosts] ||
117
+ @seeds = extract_cloud_creds(@arguments)
118
+ @seeds ||= __extract_hosts(@arguments[:hosts] ||
121
119
  @arguments[:host] ||
122
120
  @arguments[:url] ||
123
121
  @arguments[:urls] ||
@@ -130,12 +128,6 @@ module Elasticsearch
130
128
  @arguments[:transport_options][:request] = { :timeout => @arguments[:request_timeout] }
131
129
  end
132
130
 
133
- @arguments[:transport_options][:headers] ||= {}
134
-
135
- unless @arguments[:transport_options][:headers].keys.any? {|k| k.to_s.downcase =~ /content\-?\_?type/}
136
- @arguments[:transport_options][:headers]['Content-Type'] = 'application/json'
137
- end
138
-
139
131
  if @arguments[:transport]
140
132
  @transport = @arguments[:transport]
141
133
  else
@@ -162,6 +154,17 @@ module Elasticsearch
162
154
 
163
155
  private
164
156
 
157
+ def extract_cloud_creds(arguments)
158
+ return unless arguments[:cloud_id]
159
+ name = arguments[:cloud_id].split(':')[0]
160
+ cloud_url, elasticsearch_instance = Base64.decode64(arguments[:cloud_id].gsub("#{name}:", '')).split('$')
161
+ [ { scheme: 'https',
162
+ user: arguments[:user],
163
+ password: arguments[:password],
164
+ host: "#{elasticsearch_instance}.#{cloud_url}",
165
+ port: arguments[:port] || DEFAULT_CLOUD_PORT } ]
166
+ end
167
+
165
168
  # Normalizes and returns hosts configuration.
166
169
  #
167
170
  # Arrayifies the `hosts_config` argument and extracts `host` and `port` info from strings.
@@ -1,19 +1,6 @@
1
- # Licensed to Elasticsearch B.V. under one or more contributor
2
- # license agreements. See the NOTICE file distributed with
3
- # this work for additional information regarding copyright
4
- # ownership. Elasticsearch B.V. licenses this file to you under
5
- # the Apache License, Version 2.0 (the "License"); you may
6
- # not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing,
12
- # software distributed under the License is distributed on an
13
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- # KIND, either express or implied. See the License for the
15
- # specific language governing permissions and limitations
16
- # under the License.
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
17
4
 
18
5
  module Elasticsearch
19
6
  module Transport
@@ -1,19 +1,6 @@
1
- # Licensed to Elasticsearch B.V. under one or more contributor
2
- # license agreements. See the NOTICE file distributed with
3
- # this work for additional information regarding copyright
4
- # ownership. Elasticsearch B.V. licenses this file to you under
5
- # the Apache License, Version 2.0 (the "License"); you may
6
- # not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing,
12
- # software distributed under the License is distributed on an
13
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- # KIND, either express or implied. See the License for the
15
- # specific language governing permissions and limitations
16
- # under the License.
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
17
4
 
18
5
  module Elasticsearch
19
6
  module Transport
@@ -35,7 +22,7 @@ module Elasticsearch
35
22
  attr_reader :hosts, :options, :connections, :counter, :last_request_at, :protocol
36
23
  attr_accessor :serializer, :sniffer, :logger, :tracer,
37
24
  :reload_connections, :reload_after,
38
- :resurrect_after, :max_retries
25
+ :resurrect_after
39
26
 
40
27
  # Creates a new transport object
41
28
  #
@@ -56,6 +43,7 @@ module Elasticsearch
56
43
  @options[:retry_on_status] ||= []
57
44
 
58
45
  @block = block
46
+ @compression = !!@options[:compression]
59
47
  @connections = __build_connections
60
48
 
61
49
  @serializer = options[:serializer] || ( options[:serializer_class] ? options[:serializer_class].new(self) : DEFAULT_SERIALIZER_CLASS.new(self) )
@@ -71,7 +59,6 @@ module Elasticsearch
71
59
  @reload_connections = options[:reload_connections]
72
60
  @reload_after = options[:reload_connections].is_a?(Integer) ? options[:reload_connections] : DEFAULT_RELOAD_AFTER
73
61
  @resurrect_after = options[:resurrect_after] || DEFAULT_RESURRECT_AFTER
74
- @max_retries = options[:retry_on_failure].is_a?(Integer) ? options[:retry_on_failure] : DEFAULT_MAX_RETRIES
75
62
  @retry_on_status = Array(options[:retry_on_status]).map { |d| d.to_i }
76
63
  end
77
64
 
@@ -202,7 +189,7 @@ module Elasticsearch
202
189
  ( params.empty? ? '' : "&#{::Faraday::Utils::ParamsHash[params].to_query}" )
203
190
  trace_body = body ? " -d '#{__convert_to_json(body, :pretty => true)}'" : ''
204
191
  trace_command = "curl -X #{method.to_s.upcase}"
205
- trace_command += " -H '#{headers.inject('') { |memo,item| memo << item[0] + ': ' + item[1] }}'" if headers && !headers.empty?
192
+ trace_command += " -H '#{headers.collect { |k,v| "#{k}: #{v}" }.join(", ")}'" if headers && !headers.empty?
206
193
  trace_command += " '#{trace_url}'#{trace_body}\n"
207
194
  tracer.info trace_command
208
195
  tracer.debug "# #{Time.now.iso8601} [#{response.status}] (#{format('%.3f', duration)}s)\n#"
@@ -234,7 +221,8 @@ module Elasticsearch
234
221
  def __full_url(host)
235
222
  url = "#{host[:protocol]}://"
236
223
  url += "#{CGI.escape(host[:user])}:#{CGI.escape(host[:password])}@" if host[:user]
237
- url += "#{host[:host]}:#{host[:port]}"
224
+ url += "#{host[:host]}"
225
+ url += ":#{host[:port]}" if host[:port]
238
226
  url += "#{host[:path]}" if host[:path]
239
227
  url
240
228
  end
@@ -257,10 +245,17 @@ module Elasticsearch
257
245
  # @raise [ServerError] If request failed on server
258
246
  # @raise [Error] If no connection is available
259
247
  #
260
- def perform_request(method, path, params={}, body=nil, headers=nil, &block)
248
+ def perform_request(method, path, params={}, body=nil, headers=nil, opts={}, &block)
261
249
  raise NoMethodError, "Implement this method in your transport class" unless block_given?
262
250
  start = Time.now
263
251
  tries = 0
252
+ reload_on_failure = opts.fetch(:reload_on_failure, @options[:reload_on_failure])
253
+
254
+ max_retries = if opts.key?(:retry_on_failure)
255
+ opts[:retry_on_failure] === true ? DEFAULT_MAX_RETRIES : opts[:retry_on_failure]
256
+ elsif options.key?(:retry_on_failure)
257
+ options[:retry_on_failure] === true ? DEFAULT_MAX_RETRIES : options[:retry_on_failure]
258
+ end
264
259
 
265
260
  params = params.clone
266
261
 
@@ -286,7 +281,7 @@ module Elasticsearch
286
281
  rescue Elasticsearch::Transport::Transport::ServerError => e
287
282
  if response && @retry_on_status.include?(response.status)
288
283
  log_warn "[#{e.class}] Attempt #{tries} to get response from #{url}"
289
- if tries <= max_retries
284
+ if tries <= (max_retries || DEFAULT_MAX_RETRIES)
290
285
  retry
291
286
  else
292
287
  log_fatal "[#{e.class}] Cannot get response from #{url} after #{tries} tries"
@@ -301,12 +296,12 @@ module Elasticsearch
301
296
 
302
297
  connection.dead!
303
298
 
304
- if @options[:reload_on_failure] and tries < connections.all.size
299
+ if reload_on_failure and tries < connections.all.size
305
300
  log_warn "[#{e.class}] Reloading connections (attempt #{tries} of #{connections.all.size})"
306
301
  reload_connections! and retry
307
302
  end
308
303
 
309
- if @options[:retry_on_failure]
304
+ if max_retries
310
305
  log_warn "[#{e.class}] Attempt #{tries} connecting to #{connection.host.inspect}"
311
306
  if tries <= max_retries
312
307
  retry
@@ -328,7 +323,7 @@ module Elasticsearch
328
323
 
329
324
  if response.status.to_i >= 300
330
325
  __log_response method, path, params, body, url, response, nil, 'N/A', duration
331
- __trace method, path, params, headers, body, url, response, nil, 'N/A', duration if tracer
326
+ __trace method, path, params, connection.connection.headers, body, url, response, nil, 'N/A', duration if tracer
332
327
 
333
328
  # Log the failure only when `ignore` doesn't match the response status
334
329
  unless ignore.include?(response.status.to_i)
@@ -345,7 +340,7 @@ module Elasticsearch
345
340
  __log_response method, path, params, body, url, response, json, took, duration
346
341
  end
347
342
 
348
- __trace method, path, params, headers, body, url, response, json, took, duration if tracer
343
+ __trace method, path, params, connection.connection.headers, body, url, response, nil, 'N/A', duration if tracer
349
344
 
350
345
  Response.new response.status, json || response.body, response.headers
351
346
  ensure
@@ -360,6 +355,66 @@ module Elasticsearch
360
355
  def host_unreachable_exceptions
361
356
  [Errno::ECONNREFUSED]
362
357
  end
358
+
359
+ private
360
+
361
+ USER_AGENT_STR = 'User-Agent'.freeze
362
+ USER_AGENT_REGEX = /user\-?\_?agent/
363
+ CONTENT_TYPE_STR = 'Content-Type'.freeze
364
+ CONTENT_TYPE_REGEX = /content\-?\_?type/
365
+ DEFAULT_CONTENT_TYPE = 'application/json'.freeze
366
+ GZIP = 'gzip'.freeze
367
+ ACCEPT_ENCODING = 'Accept-Encoding'.freeze
368
+ GZIP_FIRST_TWO_BYTES = '1f8b'.freeze
369
+ HEX_STRING_DIRECTIVE = 'H*'.freeze
370
+ RUBY_ENCODING = '1.9'.respond_to?(:force_encoding)
371
+
372
+ def decompress_response(body)
373
+ return body unless use_compression?
374
+ return body unless gzipped?(body)
375
+
376
+ io = StringIO.new(body)
377
+ gzip_reader = if RUBY_ENCODING
378
+ Zlib::GzipReader.new(io, :encoding => 'ASCII-8BIT')
379
+ else
380
+ Zlib::GzipReader.new(io)
381
+ end
382
+ gzip_reader.read
383
+ end
384
+
385
+ def gzipped?(body)
386
+ body[0..1].unpack(HEX_STRING_DIRECTIVE)[0] == GZIP_FIRST_TWO_BYTES
387
+ end
388
+
389
+ def use_compression?
390
+ @compression
391
+ end
392
+
393
+ def apply_headers(client, options)
394
+ headers = options[:headers] || {}
395
+ headers[CONTENT_TYPE_STR] = find_value(headers, CONTENT_TYPE_REGEX) || DEFAULT_CONTENT_TYPE
396
+ headers[USER_AGENT_STR] = find_value(headers, USER_AGENT_REGEX) || user_agent_header(client)
397
+ client.headers[ACCEPT_ENCODING] = GZIP if use_compression?
398
+ client.headers.merge!(headers)
399
+ end
400
+
401
+ def find_value(hash, regex)
402
+ key_value = hash.find { |k,v| k.to_s.downcase =~ regex }
403
+ if key_value
404
+ hash.delete(key_value[0])
405
+ key_value[1]
406
+ end
407
+ end
408
+
409
+ def user_agent_header(client)
410
+ @user_agent ||= begin
411
+ meta = ["RUBY_VERSION: #{RUBY_VERSION}"]
412
+ if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
413
+ meta << "#{RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase} #{RbConfig::CONFIG['target_cpu']}"
414
+ end
415
+ "elasticsearch-ruby/#{VERSION} (#{meta.join('; ')})"
416
+ end
417
+ end
363
418
  end
364
419
  end
365
420
  end
@@ -1,19 +1,6 @@
1
- # Licensed to Elasticsearch B.V. under one or more contributor
2
- # license agreements. See the NOTICE file distributed with
3
- # this work for additional information regarding copyright
4
- # ownership. Elasticsearch B.V. licenses this file to you under
5
- # the Apache License, Version 2.0 (the "License"); you may
6
- # not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing,
12
- # software distributed under the License is distributed on an
13
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- # KIND, either express or implied. See the License for the
15
- # specific language governing permissions and limitations
16
- # under the License.
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
17
4
 
18
5
  module Elasticsearch
19
6
  module Transport
@@ -1,19 +1,6 @@
1
- # Licensed to Elasticsearch B.V. under one or more contributor
2
- # license agreements. See the NOTICE file distributed with
3
- # this work for additional information regarding copyright
4
- # ownership. Elasticsearch B.V. licenses this file to you under
5
- # the Apache License, Version 2.0 (the "License"); you may
6
- # not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing,
12
- # software distributed under the License is distributed on an
13
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- # KIND, either express or implied. See the License for the
15
- # specific language governing permissions and limitations
16
- # under the License.
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
17
4
 
18
5
  module Elasticsearch
19
6
  module Transport
@@ -1,19 +1,6 @@
1
- # Licensed to Elasticsearch B.V. under one or more contributor
2
- # license agreements. See the NOTICE file distributed with
3
- # this work for additional information regarding copyright
4
- # ownership. Elasticsearch B.V. licenses this file to you under
5
- # the Apache License, Version 2.0 (the "License"); you may
6
- # not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing,
12
- # software distributed under the License is distributed on an
13
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- # KIND, either express or implied. See the License for the
15
- # specific language governing permissions and limitations
16
- # under the License.
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
17
4
 
18
5
  module Elasticsearch
19
6
  module Transport
@@ -61,18 +48,30 @@ module Elasticsearch
61
48
  class RoundRobin
62
49
  include Base
63
50
 
51
+ # @option arguments [Connections::Collection] :connections Collection with connections.
52
+ #
53
+ def initialize(arguments = {})
54
+ super
55
+ @mutex = Mutex.new
56
+ @current = nil
57
+ end
58
+
64
59
  # Returns the next connection from the collection, rotating them in round-robin fashion.
65
60
  #
66
61
  # @return [Connections::Connection]
67
62
  #
68
63
  def select(options={})
69
- # On Ruby 1.9, Array#rotate could be used instead
70
- @current = !defined?(@current) || @current.nil? ? 0 : @current+1
71
- @current = 0 if @current >= connections.size
72
- connections[@current]
64
+ @mutex.synchronize do
65
+ conns = connections
66
+ if @current && (@current < conns.size-1)
67
+ @current += 1
68
+ else
69
+ @current = 0
70
+ end
71
+ conns[@current]
72
+ end
73
73
  end
74
74
  end
75
-
76
75
  end
77
76
  end
78
77
  end
@@ -1,19 +1,6 @@
1
- # Licensed to Elasticsearch B.V. under one or more contributor
2
- # license agreements. See the NOTICE file distributed with
3
- # this work for additional information regarding copyright
4
- # ownership. Elasticsearch B.V. licenses this file to you under
5
- # the Apache License, Version 2.0 (the "License"); you may
6
- # not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing,
12
- # software distributed under the License is distributed on an
13
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- # KIND, either express or implied. See the License for the
15
- # specific language governing permissions and limitations
16
- # under the License.
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
17
4
 
18
5
  module Elasticsearch
19
6
  module Transport