elastomer-client 3.1.2 → 3.1.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 739a7083a0d3f2499f70d7940e067caf82584f63
4
- data.tar.gz: 97f9ba36db36aa1da06f9648962b9f80d76e5e1b
3
+ metadata.gz: ee5ef212d2d3fefeef8794a4b25901237a989ded
4
+ data.tar.gz: 3434bb50acbebd5cf4a8d98a2bff8e2fe8863dd8
5
5
  SHA512:
6
- metadata.gz: c914b6adc53461678ee18f84b83856e241f2af4de326514c191c5898a53580e91f947cc74bcad6f95d9b3e71093e017efc815e5d1992b07b5d03e3856e7f24df
7
- data.tar.gz: 5d43ee3e85f1ba7a18ec0772d7a9b0935070549f1484c3aa5a8941d611963dbb265ec7034629633f50582821849152df3d8e7e79dd0e1c72ac5bb87b3a7185ea
6
+ metadata.gz: 92a3b002a675a1937360c01670df306e91af406b7bed2402f08094bf4e077d81cee56f961287ea38b702c5f0f0d994d77a4a55efe34261d47e9b5bf62d4e49f7
7
+ data.tar.gz: '02297cdcb7c4305b2d9e5e20edb7ac9247ba4f0daae03feec88c09621257d57adc5b482e9ea405cc01f7a515486898eb21f97dcb747d853fb2700ab585f93e94'
@@ -33,11 +33,14 @@ module Elastomer
33
33
  # :max_retries - the maximum number of request retires (defaults to 0)
34
34
  # :retry_delay - delay in seconds between retries (defaults to 0.075)
35
35
  # :strict_params - set to `true` to raise exceptions when invalid request params are used
36
+ # :es_version - set to the Elasticsearch version (example: "5.6.6") to avoid an HTTP request to get the version.
37
+ # :compress_body - set to true to enable request body compression (default: false)
38
+ # :compression - The compression level (0-9) when request body compression is enabled (default: Zlib::DEFAULT_COMPRESSION)
36
39
  #
37
40
  def initialize(host: "localhost", port: 9200, url: nil,
38
41
  read_timeout: 5, open_timeout: 2, max_retries: 0, retry_delay: 0.075,
39
42
  opaque_id: false, adapter: Faraday.default_adapter, max_request_size: MAX_REQUEST_SIZE,
40
- strict_params: false)
43
+ strict_params: false, es_version: nil, compress_body: false, compression: Zlib::DEFAULT_COMPRESSION)
41
44
 
42
45
  @url = url || "http://#{host}:#{port}"
43
46
 
@@ -53,12 +56,18 @@ module Elastomer
53
56
  @opaque_id = opaque_id
54
57
  @max_request_size = max_request_size
55
58
  @strict_params = strict_params
59
+ @es_version = es_version
60
+ @compress_body = compress_body
61
+ @compression = compression
56
62
  end
57
63
 
58
64
  attr_reader :host, :port, :url
59
65
  attr_reader :read_timeout, :open_timeout
60
66
  attr_reader :max_retries, :retry_delay, :max_request_size
61
67
  attr_reader :strict_params
68
+ attr_reader :es_version
69
+ attr_reader :compress_body
70
+ attr_reader :compression
62
71
  alias :strict_params? :strict_params
63
72
 
64
73
  # Returns a duplicate of this Client connection configured in the exact same
@@ -84,6 +93,8 @@ module Elastomer
84
93
 
85
94
  # Returns the version String of the attached Elasticsearch instance.
86
95
  def version
96
+ return es_version unless es_version.nil?
97
+
87
98
  @version ||= begin
88
99
  response = get "/"
89
100
  response.body.dig("version", "number")
@@ -120,6 +131,7 @@ module Elastomer
120
131
  conn.request(:encode_json)
121
132
  conn.request(:opaque_id) if @opaque_id
122
133
  conn.request(:limit_size, max_request_size: max_request_size) if max_request_size
134
+ conn.request(:elastomer_compress, compression: compression) if compress_body
123
135
 
124
136
  if @adapter.is_a?(Array)
125
137
  conn.adapter(*@adapter)
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+ require "stringio"
3
+
4
+ module Elastomer
5
+ module Middleware
6
+ # Request middleware that compresses request bodies with GZip for supported
7
+ # versions of Elasticsearch.
8
+ #
9
+ # It will only compress when there is a request body that is a String. This
10
+ # middleware should be inserted after JSON serialization.
11
+ class Compress < Faraday::Middleware
12
+ CONTENT_ENCODING = "Content-Encoding"
13
+ GZIP = "gzip"
14
+ # An Ethernet packet can hold 1500 bytes. No point in compressing anything smaller than that (plus some wiggle room).
15
+ MIN_BYTES_FOR_COMPRESSION = 1400
16
+
17
+ attr_reader :compression
18
+
19
+ # options - The Hash of "keyword" arguments.
20
+ # :compression - the compression level (0-9, default Zlib::DEFAULT_COMPRESSION)
21
+ def initialize(app, options = {})
22
+ super(app)
23
+ @compression = options[:compression] || Zlib::DEFAULT_COMPRESSION
24
+ end
25
+
26
+ def call(env)
27
+ if body = env[:body]
28
+ if body.is_a?(String) && body.bytesize > MIN_BYTES_FOR_COMPRESSION
29
+ output = StringIO.new
30
+ output.set_encoding("BINARY")
31
+ gz = Zlib::GzipWriter.new(output, compression, Zlib::DEFAULT_STRATEGY)
32
+ gz.write(env[:body])
33
+ gz.close
34
+ env[:body] = output.string
35
+ env[:request_headers][CONTENT_ENCODING] = GZIP
36
+ end
37
+ end
38
+
39
+ @app.call(env)
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ Faraday::Request.register_middleware(elastomer_compress: ::Elastomer::Middleware::Compress)
@@ -1,5 +1,5 @@
1
1
  module Elastomer
2
- VERSION = "3.1.2"
2
+ VERSION = "3.1.3"
3
3
 
4
4
  def self.version
5
5
  VERSION
@@ -161,6 +161,11 @@ module Elastomer
161
161
  end
162
162
  end
163
163
 
164
+ # ES 5.X supports GZip-compressed request bodies, but ES 2.4 doesn't?
165
+ def supports_gzip?
166
+ es_version_5_x?
167
+ end
168
+
164
169
  private
165
170
 
166
171
  # Internal: Helper to reject arguments that shouldn't be passed because
data/test/client_test.rb CHANGED
@@ -171,6 +171,15 @@ describe Elastomer::Client do
171
171
  assert_match(/[\d\.]+/, $client.version)
172
172
  end
173
173
 
174
+ it "does not make an HTTP request for version if it is provided at create time" do
175
+ request = stub_request(:get, "#{$client.url}/")
176
+
177
+ client = Elastomer::Client.new $client_params.merge(es_version: "5.6.6")
178
+ assert_equal "5.6.6", client.version
179
+
180
+ assert_not_requested request
181
+ end
182
+
174
183
  it "gets semantic version" do
175
184
  version_string = $client.version
176
185
  assert_equal Semantic::Version.new(version_string), $client.semantic_version
data/test/test_helper.rb CHANGED
@@ -43,6 +43,17 @@ raise "No server available at #{$client.url}" unless $client.available?
43
43
 
44
44
  puts "Elasticsearch version is #{$client.version}"
45
45
 
46
+ # COMPATIBILITY
47
+ # Returns true if the Elasticsearch cluster defaults to supporting compression.
48
+ def supports_compressed_bodies_by_default?
49
+ $client.version_support.es_version_5_x?
50
+ end
51
+
52
+ # Now that we have the version, re-create the client with compression if supported.
53
+ if supports_compressed_bodies_by_default?
54
+ $client = Elastomer::Client.new $client_params.merge(compress_body: true)
55
+ end
56
+
46
57
  # remove any lingering test indices from the cluster
47
58
  MiniTest.after_run do
48
59
  $client.cluster.indices.keys.each do |name|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastomer-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.2
4
+ version: 3.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Pease
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-06-11 00:00:00.000000000 Z
12
+ date: 2018-07-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: addressable
@@ -275,6 +275,7 @@ files:
275
275
  - lib/elastomer/client/template.rb
276
276
  - lib/elastomer/client/warmer.rb
277
277
  - lib/elastomer/core_ext/time.rb
278
+ - lib/elastomer/middleware/compress.rb
278
279
  - lib/elastomer/middleware/encode_json.rb
279
280
  - lib/elastomer/middleware/limit_size.rb
280
281
  - lib/elastomer/middleware/opaque_id.rb