Skip to content

Commit 7684962

Browse files
committed
[CLIENT] Apply compression headers manually based on general :compression option
1 parent 07ce621 commit 7684962

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

elasticsearch-transport/lib/elasticsearch/transport/transport/base.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def initialize(arguments={}, &block)
5656
@options[:retry_on_status] ||= []
5757

5858
@block = block
59+
@compression = !!@options[:compression]
5960
@connections = __build_connections
6061

6162
@serializer = options[:serializer] || ( options[:serializer_class] ? options[:serializer_class].new(self) : DEFAULT_SERIALIZER_CLASS.new(self) )
@@ -369,6 +370,11 @@ def host_unreachable_exceptions
369370
CONTENT_TYPE_STR = 'Content-Type'.freeze
370371
CONTENT_TYPE_REGEX = /content\-?\_?type/
371372
DEFAULT_CONTENT_TYPE = 'application/json'.freeze
373+
GZIP = 'gzip'.freeze
374+
375+
def use_compression?
376+
@compression
377+
end
372378

373379
def apply_headers(client, options)
374380
headers = options[:headers] || {}

elasticsearch-transport/lib/elasticsearch/transport/transport/http/faraday.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def perform_request(method, path, params={}, body=nil, headers=nil)
5151
# @return [Connections::Connection]
5252
#
5353
def __build_connection(host, options={}, block=nil)
54+
options.merge!(headers: { accept_encoding: 'gzip' }) if use_compression?
5455
client = ::Faraday.new(__full_url(host), options, &block)
5556
apply_headers(client, options)
5657
Connections::Connection.new :host => host, :connection => client
@@ -73,7 +74,7 @@ def host_unreachable_exceptions
7374

7475
def decompress_response(response)
7576
body = response.body
76-
return body unless response.headers && response.headers['content-encoding'] == GZIP
77+
return body unless use_compression?
7778
return body unless gzipped?(body)
7879

7980
io = StringIO.new(body)

elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,8 @@
12441244
context 'when using the Net::HTTP adapter' do
12451245

12461246
let!(:client) do
1247-
described_class.new(transport_options: { headers: { accept_encoding: 'gzip' } }, adapter: :net_http)
1247+
#described_class.new(transport_options: { headers: { accept_encoding: 'gzip' } }, adapter: :net_http)
1248+
described_class.new(hosts: ELASTICSEARCH_HOSTS, compression: true, adapter: :net_http)
12481249
end
12491250

12501251
it 'sets the options on the transport' do
@@ -1255,7 +1256,7 @@
12551256
context 'when using the HTTPClient adapter' do
12561257

12571258
let!(:client) do
1258-
described_class.new(transport_options: { headers: { accept_encoding: 'gzip' } }, adapter: :httpclient)
1259+
described_class.new(hosts: ELASTICSEARCH_HOSTS, compression: true, adapter: :httpclient)
12591260
end
12601261

12611262
it 'sets the options on the transport' do
@@ -1266,7 +1267,7 @@
12661267
context 'when using the Patron adapter' do
12671268

12681269
let!(:client) do
1269-
described_class.new(transport_options: { headers: { accept_encoding: 'gzip' } }, adapter: :patron)
1270+
described_class.new(hosts: ELASTICSEARCH_HOSTS, compression: true, adapter: :patron)
12701271
end
12711272

12721273
it 'sets the options on the transport' do
@@ -1277,7 +1278,7 @@
12771278
context 'when using the Net::HTTP::Persistent adapter' do
12781279

12791280
let!(:client) do
1280-
described_class.new(transport_options: { headers: { accept_encoding: 'gzip' } }, adapter: :net_http_persistent)
1281+
described_class.new(hosts: ELASTICSEARCH_HOSTS, compression: true, adapter: :net_http_persistent)
12811282
end
12821283

12831284
it 'sets the options on the transport' do
@@ -1288,7 +1289,7 @@
12881289
context 'when using the Typhoeus adapter' do
12891290

12901291
let!(:client) do
1291-
described_class.new(transport_options: { headers: { accept_encoding: 'gzip' } }, adapter: :typhoeus)
1292+
described_class.new(hosts: ELASTICSEARCH_HOSTS, compression: true, adapter: :typhoeus)
12921293
end
12931294

12941295
it 'sets the options on the transport' do
@@ -1307,7 +1308,7 @@
13071308
client.perform_request('DELETE', 'myindex') rescue
13081309
client.perform_request('PUT', 'myindex', {}, { settings: { number_of_shards: 2, number_of_replicas: 0 } })
13091310
client.perform_request('PUT', 'myindex/mydoc/1', { routing: 'XYZ', timeout: '1s' }, { foo: 'bar' })
1310-
client.perform_request('GET', '_cluster/health?wait_for_status=green', {})
1311+
client.perform_request('GET', '_cluster/health?wait_for_status=green&timeout=2s', {})
13111312
end
13121313

13131314
let(:response) do

0 commit comments

Comments
 (0)