elastic-transport 8.1.0 → 8.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/tests.yml +4 -4
- data/CHANGELOG.md +10 -1
- data/README.md +7 -518
- data/Rakefile +9 -0
- data/lib/elastic/transport/transport/base.rb +55 -50
- data/lib/elastic/transport/transport/errors.rb +2 -4
- data/lib/elastic/transport/transport/http/faraday.rb +29 -27
- data/lib/elastic/transport/transport/response.rb +3 -2
- data/lib/elastic/transport/version.rb +1 -1
- data/spec/elastic/transport/base_spec.rb +22 -23
- data/spec/elastic/transport/client_spec.rb +3 -13
- data/spec/spec_helper.rb +0 -1
- data/test/test_helper.rb +0 -1
- data/test/unit/transport_base_test.rb +7 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aca3fc15486eeb7455f903dbaa5b6a594d4fd410f895cff6b3bf832c0dcad8b4
|
4
|
+
data.tar.gz: 4fd798b71008e3bba436434a5c98321fcd2b15934a6244908966c8b9cdd5c3e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a33a557b17fc75fbc2cd9b8fb713603b67c35097fce929ccdee0b81b76c58c05885286aeb1aeed8c72068d6c12a83b51e6dc1f9f07678d6e17f51ce962cb0af8
|
7
|
+
data.tar.gz: 5105d1a01106314206e250a105329abfb9553f6c12170bafaf8b7eb4741f5d7b9b414027919aac9df9867ee0859fbfce6b492fbc64a8335f17eebcc7fd109cf4
|
data/.github/workflows/tests.yml
CHANGED
@@ -15,8 +15,8 @@ jobs:
|
|
15
15
|
strategy:
|
16
16
|
fail-fast: false
|
17
17
|
matrix:
|
18
|
-
ruby: [ '2.7', '3.0', '3.1', 'jruby-9.3' ]
|
19
|
-
es_version: ['8.
|
18
|
+
ruby: [ '2.7', '3.0', '3.1', '3.2', 'jruby-9.3', 'jruby-9.4' ]
|
19
|
+
es_version: ['8.4-SNAPSHOT', '8.5-SNAPSHOT', '8.6-SNAPSHOT']
|
20
20
|
runs-on: ubuntu-latest
|
21
21
|
steps:
|
22
22
|
- uses: actions/checkout@v2
|
@@ -53,8 +53,8 @@ jobs:
|
|
53
53
|
strategy:
|
54
54
|
fail-fast: false
|
55
55
|
matrix:
|
56
|
-
ruby: [ '2.7', '3.0', '3.1', 'jruby-9.3' ]
|
57
|
-
es_version: ['8.
|
56
|
+
ruby: [ '2.7', '3.0', '3.1', '3.2', 'jruby-9.3' ]
|
57
|
+
es_version: ['8.6-SNAPSHOT']
|
58
58
|
runs-on: ubuntu-latest
|
59
59
|
steps:
|
60
60
|
- uses: actions/checkout@v2
|
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,19 @@
|
|
1
|
+
## 8.2.0
|
2
|
+
|
3
|
+
Tested versions of Ruby: (MRI) 2.7, 3.0, 3.1, 3.2, JRuby 9.3 and JRuby 9.4.
|
4
|
+
|
5
|
+
- Fixes [issue #44](https://github.com/elastic/elastic-transport-ruby/issues/44), raising `Elasticsearch::Transport::Transport::Error` instead of `Faraday::ConnectionFailed` (or any other Faraday error class) when a host is unreachable.
|
6
|
+
- Removes development dependency on `ansi`, it hasn't been updated for years.
|
7
|
+
- Adds `rake console` task to run IRB with the Elastic transport client libraries loaded.
|
8
|
+
- General refactors, cleanup and updates in code for `base.rb` (The base class used by HTTP implementations Faraday, Manticore, Curb), `errors.rb`, `faraday.rb`, and more.
|
9
|
+
|
1
10
|
## 8.1.0
|
2
11
|
|
3
12
|
Adds support for Faraday version 2. From [Faraday's Upgrading guide](https://github.com/lostisland/faraday/blob/main/UPGRADING.md#faraday-20), the main change is the adapters have moved:
|
4
13
|
|
5
14
|
> With this release, we've officially moved all adapters, except for the net_http one, out of Faraday. What that means, is that they won't be available out-of-the-box anymore, and you'll instead need to add them to your Gemfile.
|
6
15
|
> If you just use the default net_http adapter, then you don't need to do anything!
|
7
|
-
> Otherwise, add the corresponding adapter gem to your Gemfile (e.g. faraday-net_http_persistent). Then, simply require them after you require faraday.
|
16
|
+
> Otherwise, add the corresponding adapter gem to your Gemfile (e.g. faraday-net_http_persistent). Then, simply require them after you require faraday.
|
8
17
|
|
9
18
|
We're now supporting Faraday v2 and Faraday v1. The adapters were removed as development dependency in the gemspec and added to the Gemfile. A new file `Gemfile-faraday1.gemfile` was added to run tests with version `1.x` of Faraday too.
|
10
19
|
|
data/README.md
CHANGED
@@ -3,63 +3,7 @@
|
|
3
3
|
|
4
4
|
This gem provides a low-level Ruby client for connecting to an [Elastic](http://elastic.co) cluster. It powers both the [Elasticsearch client](https://github.com/elasticsearch/elasticsearch-ruby/) and the [Elastic Enterprise Search](https://github.com/elastic/enterprise-search-ruby/) client.
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
This gem is compatible with maintained Ruby versions. See [Ruby Maintenance Branches](https://www.ruby-lang.org/en/downloads/branches/). We don't provide support to versions which have reached their end of life.
|
9
|
-
|
10
|
-
## Installation
|
11
|
-
|
12
|
-
Install the package from [Rubygems](https://rubygems.org):
|
13
|
-
|
14
|
-
gem install elastic-transport
|
15
|
-
|
16
|
-
To use an unreleased version, either add it to your `Gemfile` for [Bundler](http://gembundler.com):
|
17
|
-
|
18
|
-
gem 'elastic-transport', git: 'git://github.com/elastic/elastic-transport-ruby.git'
|
19
|
-
|
20
|
-
or install it from a source code checkout:
|
21
|
-
|
22
|
-
```bash
|
23
|
-
git clone https://github.com/elastic/elastic-transport-ruby.git
|
24
|
-
cd elastic-transport-ruby
|
25
|
-
bundle install
|
26
|
-
rake install
|
27
|
-
```
|
28
|
-
|
29
|
-
## Description
|
30
|
-
|
31
|
-
It handles connecting to multiple nodes in the cluster, rotating across connections, logging and tracing requests and responses, maintaining failed connections, discovering nodes in the cluster, and provides an abstraction for
|
32
|
-
data serialization and transport.
|
33
|
-
|
34
|
-
It does not handle calling the Elasticsearch API; see the [`elasticsearch`](https://github.com/elasticsearch/elasticsearch-ruby) library for that.
|
35
|
-
|
36
|
-
Features overview:
|
37
|
-
|
38
|
-
* Pluggable logging and tracing
|
39
|
-
* Pluggable connection selection strategies (round-robin, random, custom)
|
40
|
-
* Pluggable transport implementation, customizable and extendable
|
41
|
-
* Pluggable serializer implementation
|
42
|
-
* Request retries and dead connections handling
|
43
|
-
* Node reloading (based on cluster state) on errors or on demand
|
44
|
-
|
45
|
-
This library uses [Faraday](https://github.com/lostisland/faraday) by default as the HTTP transport implementation. We test it it with Faraday versions 1.x and Faraday 2.x.
|
46
|
-
|
47
|
-
For optimal performance, use a HTTP library which supports persistent ("keep-alive") connections, such as [patron](https://github.com/toland/patron) or [Typhoeus](https://github.com/typhoeus/typhoeus).
|
48
|
-
Require the library (`require 'patron'`) in your code for Faraday 1.x or the adapter (`require 'faraday/patron'`) for Faraday 2.x, and it will be automatically used.
|
49
|
-
|
50
|
-
Currently these libraries are supported:
|
51
|
-
- [Patron](https://github.com/toland/patron)
|
52
|
-
- [Typhoeus](https://github.com/typhoeus/typhoeus)
|
53
|
-
- [HTTPClient](https://rubygems.org/gems/httpclient)
|
54
|
-
- [Net::HTTP::Persistent](https://rubygems.org/gems/net-http-persistent)
|
55
|
-
|
56
|
-
**Note on [Typhoeus](https://github.com/typhoeus/typhoeus)**: You need to use v1.4.0 or up since older versions are not compatible with Faraday 1.0.
|
57
|
-
|
58
|
-
You can customize Faraday and implement your own HTTP transport. For detailed information, see the example configurations and more information [below](#transport-implementations).
|
59
|
-
|
60
|
-
## Example Usage
|
61
|
-
|
62
|
-
In the simplest form, connect to Elasticsearch running on <http://localhost:9200> without any configuration:
|
6
|
+
In the simplest form, connect to Elasticsearch running on `http://localhost:9200` without any configuration:
|
63
7
|
|
64
8
|
```ruby
|
65
9
|
require 'elastic/transport'
|
@@ -69,455 +13,13 @@ response = client.perform_request('GET', '_cluster/health')
|
|
69
13
|
# => #<Elastic::Transport::Transport::Response:0x007fc5d506ce38 @status=200, @body={ ... } >
|
70
14
|
```
|
71
15
|
|
72
|
-
|
73
|
-
|
74
|
-
## Configuration
|
75
|
-
|
76
|
-
* [Setting Hosts](#setting-hosts)
|
77
|
-
* [Default port](#default-port)
|
78
|
-
* [Authentication](#authentication)
|
79
|
-
* [Logging](#logging)
|
80
|
-
* [Custom HTTP Headers](#custom-http-headers)
|
81
|
-
* [Setting Timeouts](#setting-timeouts)
|
82
|
-
* [Randomizing Hosts](#randomizing-hosts)
|
83
|
-
* [Retrying on Failures](#retrying-on-failures)
|
84
|
-
* [Reloading Hosts](#reloading-hosts)
|
85
|
-
* [Connection Selector](#connection-selector)
|
86
|
-
* [Transport Implementations](#transport-implementations)
|
87
|
-
* [Serializer implementations](#serializer-implementations)
|
88
|
-
* [Exception Handling](#exception-handling)
|
89
|
-
* [Development and Community](#development-and-community)
|
90
|
-
|
91
|
-
The client supports many configurations options for setting up and managing connections,
|
92
|
-
configuring logging, customizing the transport library, etc.
|
93
|
-
|
94
|
-
### Setting Hosts
|
95
|
-
|
96
|
-
This behaviour is going to be simplified, see [#5](https://github.com/elastic/elastic-transport-ruby/issues/5). To connect to a specific Elasticsearch host:
|
97
|
-
|
98
|
-
```ruby
|
99
|
-
Elastic::Transport::Client.new(host: 'search.myserver.com')
|
100
|
-
```
|
101
|
-
|
102
|
-
To connect to a host with specific port:
|
103
|
-
|
104
|
-
```ruby
|
105
|
-
Elastic::Transport::Client.new(host: 'myhost:8080')
|
106
|
-
```
|
107
|
-
|
108
|
-
To connect to multiple hosts:
|
109
|
-
|
110
|
-
```ruby
|
111
|
-
Elastic::Transport::Client.new(hosts: ['myhost1', 'myhost2'])
|
112
|
-
```
|
113
|
-
|
114
|
-
Instead of Strings, you can pass host information as an array of Hashes:
|
115
|
-
|
116
|
-
```ruby
|
117
|
-
Elastic::Transport::Client.new(hosts: [{ host: 'myhost1', port: 8080 }, { host: 'myhost2', port: 8080 }])
|
118
|
-
```
|
119
|
-
|
120
|
-
**NOTE:** When specifying multiple hosts, you probably want to enable the `retry_on_failure` or `retry_on_status` options to perform a failed request on another node (see the _Retrying on Failures_ chapter).
|
121
|
-
|
122
|
-
Common URL parts -- scheme, HTTP authentication credentials, URL prefixes, etc -- are handled automatically:
|
123
|
-
```ruby
|
124
|
-
Elastic::Transport::Client.new(url: 'https://username:password@api.server.org:4430/search')
|
125
|
-
```
|
126
|
-
|
127
|
-
You can pass multiple URLs separated by a comma:
|
128
|
-
```ruby
|
129
|
-
Elastic::Transport::Client.new(urls: 'http://localhost:9200,http://localhost:9201')
|
130
|
-
```
|
131
|
-
|
132
|
-
Another way to configure the URL(s) is to export the `ELASTICSEARCH_URL` variable.
|
133
|
-
|
134
|
-
The client will automatically round-robin across the hosts (unless you select or implement a different [connection selector](#connection-selector)).
|
135
|
-
|
136
|
-
### Default port
|
137
|
-
|
138
|
-
The default port is `9200`. Please specify a port for your host(s) if they differ from this default. Please see below for an exception to this when connecting using an Elastic Cloud ID.
|
139
|
-
|
140
|
-
### Authentication
|
141
|
-
|
142
|
-
You can pass the authentication credentials, scheme and port in the host configuration hash:
|
143
|
-
|
144
|
-
```ruby
|
145
|
-
Elastic::Transport::Client.new(
|
146
|
-
hosts: [
|
147
|
-
{
|
148
|
-
host: 'my-protected-host',
|
149
|
-
port: '443',
|
150
|
-
user: 'USERNAME',
|
151
|
-
password: 'PASSWORD',
|
152
|
-
scheme: 'https'
|
153
|
-
}
|
154
|
-
]
|
155
|
-
)
|
156
|
-
```
|
157
|
-
Or use the common URL format:
|
158
|
-
|
159
|
-
```ruby
|
160
|
-
Elastic::Transport::Client.new(url: 'https://username:password@example.com:9200')
|
161
|
-
```
|
162
|
-
|
163
|
-
To pass a custom certificate for SSL peer verification to Faraday-based clients, use the `transport_options` option:
|
164
|
-
|
165
|
-
```ruby
|
166
|
-
Elastic::Transport::Client.new(
|
167
|
-
url: 'https://username:password@example.com:9200',
|
168
|
-
transport_options: { ssl: { ca_file: '/path/to/cacert.pem' } }
|
169
|
-
)
|
170
|
-
```
|
171
|
-
|
172
|
-
### Logging
|
173
|
-
|
174
|
-
To log requests and responses to standard output with the default logger (an instance of Ruby's {::Logger} class), set the `log` argument to true:
|
175
|
-
|
176
|
-
```ruby
|
177
|
-
Elastic::Transport::Client.new(log: true)
|
178
|
-
```
|
179
|
-
|
180
|
-
You can also use [ecs-logging](https://github.com/elastic/ecs-logging-ruby). `ecs-logging` is a set of libraries that allows you to transform your application logs to structured logs that comply with the [Elastic Common Schema (ECS)](https://www.elastic.co/guide/en/ecs/current/ecs-reference.html):
|
181
|
-
|
182
|
-
```ruby
|
183
|
-
logger = EcsLogging::Logger.new($stdout)
|
184
|
-
Elastic::Transport::Client.new(logger: logger)
|
185
|
-
```
|
186
|
-
|
187
|
-
To trace requests and responses in the _Curl_ format, set the `trace` argument:
|
188
|
-
|
189
|
-
```ruby
|
190
|
-
Elastic::Transport::Client.new(trace: true)
|
191
|
-
```
|
192
|
-
|
193
|
-
You can customize the default logger or tracer:
|
194
|
-
|
195
|
-
```ruby
|
196
|
-
client.transport.logger.formatter = proc { |s, d, p, m| "#{s}: #{m}\n" }
|
197
|
-
client.transport.logger.level = Logger::INFO
|
198
|
-
```
|
199
|
-
|
200
|
-
Or, you can use a custom `::Logger` instance:
|
201
|
-
|
202
|
-
```ruby
|
203
|
-
Elastic::Transport::Client.new(logger: Logger.new(STDERR))
|
204
|
-
```
|
205
|
-
|
206
|
-
You can pass the client any conforming logger implementation:
|
207
|
-
|
208
|
-
```ruby
|
209
|
-
require 'logging' # https://github.com/TwP/logging/
|
210
|
-
|
211
|
-
log = Logging.logger['elasticsearch']
|
212
|
-
log.add_appenders Logging.appenders.stdout
|
213
|
-
log.level = :info
|
214
|
-
|
215
|
-
client = Elastic::Transport::Client.new(logger: log)
|
216
|
-
```
|
217
|
-
|
218
|
-
### Custom HTTP Headers
|
219
|
-
|
220
|
-
You can set a custom HTTP header on the client's initializer:
|
221
|
-
|
222
|
-
```ruby
|
223
|
-
client = Elastic::Transport::Client.new(
|
224
|
-
transport_options: {
|
225
|
-
headers:
|
226
|
-
{user_agent: "My App"}
|
227
|
-
}
|
228
|
-
)
|
229
|
-
```
|
230
|
-
|
231
|
-
You can also pass in `headers` as a parameter to any of the API Endpoints to set custom headers for the request:
|
232
|
-
|
233
|
-
```ruby
|
234
|
-
client.search(index: 'myindex', q: 'title:test', headers: { user_agent: "My App" })
|
235
|
-
```
|
236
|
-
|
237
|
-
### Setting Timeouts
|
238
|
-
|
239
|
-
For many operations in Elasticsearch, the default timeouts of HTTP libraries are too low.
|
240
|
-
To increase the timeout, you can use the `request_timeout` parameter:
|
241
|
-
|
242
|
-
```ruby
|
243
|
-
Elastic::Transport::Client.new(request_timeout: 5 * 60)
|
244
|
-
```
|
245
|
-
|
246
|
-
You can also use the `transport_options` argument documented below.
|
247
|
-
|
248
|
-
### Randomizing Hosts
|
249
|
-
|
250
|
-
If you pass multiple hosts to the client, it rotates across them in a round-robin fashion, by default.
|
251
|
-
When the same client would be running in multiple processes (eg. in a Ruby web server such as Thin),
|
252
|
-
it might keep connecting to the same nodes "at once". To prevent this, you can randomize the hosts
|
253
|
-
collection on initialization and reloading:
|
254
|
-
|
255
|
-
```ruby
|
256
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], randomize_hosts: true)
|
257
|
-
```
|
258
|
-
|
259
|
-
### Retrying on Failures
|
260
|
-
|
261
|
-
When the client is initialized with multiple hosts, it makes sense to retry a failed request
|
262
|
-
on a different host:
|
263
|
-
|
264
|
-
```ruby
|
265
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], retry_on_failure: true)
|
266
|
-
```
|
16
|
+
**Refer to [the official documentation on Elastic Transport](https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/transport.html).**
|
267
17
|
|
268
|
-
|
18
|
+
**Refer to [Advanced Configuration](https://elasticsearch-ruby_1881.docs-preview.app.elstc.co/guide/en/elasticsearch/client/ruby-api/master/advanced-config.html) to read about more configuration options.**
|
269
19
|
|
270
|
-
|
271
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], retry_on_failure: 5)
|
272
|
-
```
|
273
|
-
|
274
|
-
You can also use `retry_on_status` to retry when specific status codes are returned:
|
275
|
-
|
276
|
-
```ruby
|
277
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], retry_on_status: [502, 503])
|
278
|
-
```
|
279
|
-
|
280
|
-
These two parameters can also be used together:
|
281
|
-
|
282
|
-
```ruby
|
283
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], retry_on_status: [502, 503], retry_on_failure: 10)
|
284
|
-
```
|
285
|
-
|
286
|
-
### Reloading Hosts
|
287
|
-
|
288
|
-
Elasticsearch by default dynamically discovers new nodes in the cluster. You can leverage this in the client, and periodically check for new nodes to spread the load.
|
289
|
-
|
290
|
-
To retrieve and use the information from the [_Nodes Info API_](http://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html) on every 10,000th request:
|
291
|
-
|
292
|
-
```ruby
|
293
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], reload_connections: true)
|
294
|
-
```
|
295
|
-
|
296
|
-
You can pass a specific number of requests after which the reloading should be performed:
|
297
|
-
|
298
|
-
```ruby
|
299
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], reload_connections: 1_000)
|
300
|
-
```
|
301
|
-
|
302
|
-
To reload connections on failures, use:
|
303
|
-
|
304
|
-
```ruby
|
305
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], reload_on_failure: true)
|
306
|
-
```
|
307
|
-
|
308
|
-
The reloading will timeout if not finished under 1 second by default. To change the setting:
|
309
|
-
|
310
|
-
```ruby
|
311
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], sniffer_timeout: 3)
|
312
|
-
```
|
313
|
-
|
314
|
-
**NOTE:** When using reloading hosts ("sniffing") together with authentication, just pass the scheme, user and password with the host info -- or, for more clarity, in the `http` options:
|
315
|
-
|
316
|
-
```ruby
|
317
|
-
Elastic::Transport::Client.new(
|
318
|
-
host: 'localhost:9200',
|
319
|
-
http: { scheme: 'https', user: 'U', password: 'P' },
|
320
|
-
reload_connections: true,
|
321
|
-
reload_on_failure: true
|
322
|
-
)
|
323
|
-
```
|
324
|
-
|
325
|
-
### Connection Selector
|
326
|
-
|
327
|
-
By default, the client will rotate the connections in a round-robin fashion, using the {Elastic::Transport::Transport::Connections::Selector::RoundRobin} strategy.
|
328
|
-
|
329
|
-
You can implement your own strategy to customize the behaviour. For example, let's have a "rack aware" strategy, which will prefer the nodes with a specific [attribute](https://github.com/elasticsearch/elasticsearch/blob/1.0/config/elasticsearch.yml#L81-L85). Only when these would be unavailable, the strategy will use the other nodes:
|
330
|
-
|
331
|
-
```ruby
|
332
|
-
class RackIdSelector
|
333
|
-
include Elastic::Transport::Transport::Connections::Selector::Base
|
334
|
-
|
335
|
-
def select(options={})
|
336
|
-
connections.select do |c|
|
337
|
-
# Try selecting the nodes with a `rack_id:x1` attribute first
|
338
|
-
c.host[:attributes] && c.host[:attributes][:rack_id] == 'x1'
|
339
|
-
end.sample || connections.to_a.sample
|
340
|
-
end
|
341
|
-
end
|
342
|
-
|
343
|
-
Elastic::Transport::Client.new hosts: ['x1.search.org', 'x2.search.org'], selector_class: RackIdSelector
|
344
|
-
```
|
345
|
-
|
346
|
-
### Transport Implementations
|
347
|
-
|
348
|
-
By default, the client will use the [_Faraday_](https://rubygems.org/gems/faraday) HTTP library as a transport implementation.
|
349
|
-
|
350
|
-
It will auto-detect and use an _adapter_ for _Faraday_ based on gems loaded in your code, preferring HTTP clients with support for persistent connections. Faraday 2 changed the way adapters are used ([read more here](https://github.com/lostisland/faraday/blob/main/UPGRADING.md#adapters-have-moved)). If you're using Faraday 1.x, you can require the HTTP library. To use the [_Patron_](https://github.com/toland/patron) HTTP, for example, require it:
|
351
|
-
|
352
|
-
```ruby
|
353
|
-
require 'patron'
|
354
|
-
```
|
355
|
-
|
356
|
-
If you're using Faraday 2.x, you need to add the corresponding adapter gem to your Gemfile and require them after you require `faraday`:
|
357
|
-
|
358
|
-
```ruby
|
359
|
-
# Gemfile
|
360
|
-
gem 'faraday-patron'
|
361
|
-
|
362
|
-
# Code
|
363
|
-
require 'faraday'
|
364
|
-
require 'faraday/patron'
|
365
|
-
```
|
366
|
-
|
367
|
-
Then, create a new client, and the _Patron_ gem will be used as the "driver":
|
368
|
-
|
369
|
-
```ruby
|
370
|
-
client = Elastic::Transport::Client.new
|
371
|
-
|
372
|
-
client.transport.connections.first.connection.builder.adapter
|
373
|
-
# => Faraday::Adapter::Patron
|
374
|
-
|
375
|
-
10.times do
|
376
|
-
client.nodes.stats(metric: 'http')['nodes'].values.each do |n|
|
377
|
-
puts "#{n['name']} : #{n['http']['total_opened']}"
|
378
|
-
end
|
379
|
-
end
|
380
|
-
|
381
|
-
# => Stiletoo : 24
|
382
|
-
# => Stiletoo : 24
|
383
|
-
# => Stiletoo : 24
|
384
|
-
# => ...
|
385
|
-
```
|
386
|
-
|
387
|
-
To use a specific adapter for _Faraday_, pass it as the `adapter` argument:
|
388
|
-
|
389
|
-
```ruby
|
390
|
-
# Gemfile
|
391
|
-
gem 'faraday-net_http_persistent'
|
392
|
-
|
393
|
-
# Code
|
394
|
-
client = Elastic::Transport::Client.new(adapter: :net_http_persistent)
|
395
|
-
|
396
|
-
client.transport.connections.first.connection.builder.handlers
|
397
|
-
# => [Faraday::Adapter::NetHttpPersistent]
|
398
|
-
```
|
399
|
-
|
400
|
-
To pass options to the
|
401
|
-
[`Faraday::Connection`](https://github.com/lostisland/faraday/blob/master/lib/faraday/connection.rb)
|
402
|
-
constructor, use the `transport_options` key:
|
403
|
-
|
404
|
-
```ruby
|
405
|
-
client = Elastic::Transport::Client.new(
|
406
|
-
transport_options: {
|
407
|
-
request: { open_timeout: 1 },
|
408
|
-
headers: { user_agent: 'MyApp' },
|
409
|
-
params: { :format => 'yaml' },
|
410
|
-
ssl: { verify: false }
|
411
|
-
}
|
412
|
-
)
|
413
|
-
```
|
414
|
-
|
415
|
-
To configure the _Faraday_ instance directly, use a block:
|
416
|
-
|
417
|
-
```ruby
|
418
|
-
require 'patron'
|
419
|
-
|
420
|
-
client = Elastic::Transport::Client.new(host: 'localhost', port: '9200') do |f|
|
421
|
-
f.response :logger
|
422
|
-
f.adapter :patron
|
423
|
-
end
|
424
|
-
```
|
425
|
-
|
426
|
-
You can use any standard Faraday middleware and plugins in the configuration block. You can also initialize the transport class yourself, and pass it to the client constructor as the `transport` argument:
|
427
|
-
|
428
|
-
```ruby
|
429
|
-
require 'patron'
|
430
|
-
|
431
|
-
transport_configuration = lambda do |f|
|
432
|
-
f.response :logger
|
433
|
-
f.adapter :patron
|
434
|
-
end
|
435
|
-
|
436
|
-
transport = Elastic::Transport::Transport::HTTP::Faraday.new(
|
437
|
-
hosts: [ { host: 'localhost', port: '9200' } ],
|
438
|
-
&transport_configuration
|
439
|
-
)
|
440
|
-
|
441
|
-
# Pass the transport to the client
|
442
|
-
#
|
443
|
-
client = Elastic::Transport::Client.new(transport: transport)
|
444
|
-
```
|
445
|
-
|
446
|
-
Instead of passing the transport to the constructor, you can inject it at run time:
|
447
|
-
|
448
|
-
```ruby
|
449
|
-
# Set up the transport
|
450
|
-
#
|
451
|
-
faraday_configuration = lambda do |f|
|
452
|
-
f.instance_variable_set :@ssl, { verify: false }
|
453
|
-
f.adapter :excon
|
454
|
-
end
|
455
|
-
|
456
|
-
faraday_client = Elastic::Transport::Transport::HTTP::Faraday.new(
|
457
|
-
hosts: [
|
458
|
-
{
|
459
|
-
host: 'my-protected-host',
|
460
|
-
port: '443',
|
461
|
-
user: 'USERNAME',
|
462
|
-
password: 'PASSWORD',
|
463
|
-
scheme: 'https'
|
464
|
-
}
|
465
|
-
],
|
466
|
-
&faraday_configuration
|
467
|
-
)
|
468
|
-
|
469
|
-
# Create a default client
|
470
|
-
#
|
471
|
-
client = Elastic::Transport::Client.new
|
472
|
-
|
473
|
-
# Inject the transport to the client
|
474
|
-
#
|
475
|
-
client.transport = faraday_client
|
476
|
-
```
|
477
|
-
|
478
|
-
You can also use a bundled [_Curb_](https://rubygems.org/gems/curb) based transport implementation:
|
479
|
-
|
480
|
-
```ruby
|
481
|
-
require 'curb'
|
482
|
-
require 'elastic/transport/transport/http/curb'
|
483
|
-
|
484
|
-
client = Elastic::Transport::Client.new(transport_class: Elastic::Transport::Transport::HTTP::Curb)
|
485
|
-
|
486
|
-
client.transport.connections.first.connection
|
487
|
-
# => #<Curl::Easy http://localhost:9200/>
|
488
|
-
```
|
489
|
-
|
490
|
-
It's possible to customize the _Curb_ instance by passing a block to the constructor as well (in this case, as an inline block):
|
491
|
-
|
492
|
-
```ruby
|
493
|
-
transport = Elastic::Transport::Transport::HTTP::Curb.new(
|
494
|
-
hosts: [ { host: 'localhost', port: '9200' } ],
|
495
|
-
& lambda { |c| c.verbose = true }
|
496
|
-
)
|
497
|
-
|
498
|
-
client = Elastic::Transport::Client.new(transport: transport)
|
499
|
-
```
|
500
|
-
|
501
|
-
You can write your own transport implementation by including the `Elastic::Transport::Transport::Base` module, implementing the required contract, and passing it to the client as the `transport_class` parameter -- or injecting it directly.
|
502
|
-
|
503
|
-
### Serializer Implementations
|
504
|
-
|
505
|
-
By default, the [MultiJSON](http://rubygems.org/gems/multi_json) library is used as the serializer implementation, and it will pick up the "right" adapter based on gems available.
|
506
|
-
|
507
|
-
The serialization component is pluggable, though, so you can write your own by including the `Elastic::Transport::Transport::Serializer::Base` module, implementing the required contract, and passing it to the client as the `serializer_class` or `serializer` parameter.
|
508
|
-
|
509
|
-
### Exception Handling
|
510
|
-
|
511
|
-
The library defines a [number of exception classes](https://github.com/elastic/elastic-transport-ruby/blob/main/lib/elastic/transport/transport/errors.rb) for various client and server errors, as well as unsuccessful HTTP responses,
|
512
|
-
making it possible to `rescue` specific exceptions with desired granularity.
|
513
|
-
|
514
|
-
The highest-level exception is `Elastic::Transport::Transport::Error` and will be raised for any generic client *or* server errors.
|
515
|
-
|
516
|
-
`Elastic::Transport::Transport::ServerError` will be raised for server errors only.
|
517
|
-
|
518
|
-
As an example for response-specific errors, a `404` response status will raise an `Elastic::Transport::Transport::Errors::NotFound` exception.
|
20
|
+
## Compatibility
|
519
21
|
|
520
|
-
|
22
|
+
This gem is compatible with maintained Ruby versions. See [Ruby Maintenance Branches](https://www.ruby-lang.org/en/downloads/branches/). We don't provide support to versions which have reached their end of life.
|
521
23
|
|
522
24
|
## Development and Community
|
523
25
|
|
@@ -525,21 +27,6 @@ For local development, clone the repository and run `bundle install`. See `rake
|
|
525
27
|
|
526
28
|
Bug fixes and features must be covered by unit tests.
|
527
29
|
|
528
|
-
Github's pull requests and issues are used to communicate, send bug reports and code contributions.
|
529
|
-
|
530
|
-
## The Architecture
|
531
|
-
|
532
|
-
* `Elastic::Transport::Client` is composed of `Elastic::Transport::Transport`.
|
533
|
-
* `Elastic::Transport::Transport` is composed of `Elastic::Transport::Transport::Connections`, and an instance of logger, tracer, serializer and sniffer.
|
534
|
-
* Logger and tracer can be any object conforming to the Ruby logging interface, ie. an instance of [`Logger`](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html), [_log4r_](https://rubygems.org/gems/log4r), [_logging_](https://github.com/TwP/logging/), etc.
|
535
|
-
* The `Elastic::Transport::Transport::Serializer::Base` implementations handles converting data for Elasticsearch (eg. to JSON). You can implement your own serializer.
|
536
|
-
* `Elastic::Transport::Transport::Sniffer` allows discovering nodes in the cluster and use them as connections.
|
537
|
-
* `Elastic::Transport::Transport::Connections::Collection` is composed of `Elastic::Transport::Transport::Connections::Connection` instances and a selector instance.
|
538
|
-
* `Elastic::Transport::Transport::Connections::Connection` contains the connection attributes such as hostname and port, as well as the concrete persistent "session" connected to a specific node.
|
539
|
-
* The `Elastic::Transport::Transport::Connections::Selector::Base` implementations allows you to choose connections from the pool, eg. in a round-robin or random fashion. You can implement your own selector strategy.
|
540
|
-
|
541
|
-
## Development
|
542
|
-
|
543
30
|
A rake task is included to launch an Elasticsearch cluster with Docker. You need to install docker on your system and then run:
|
544
31
|
```bash
|
545
32
|
$ rake docker:start[VERSION]
|
@@ -561,6 +48,8 @@ time rake test:integration
|
|
561
48
|
|
562
49
|
Use `COVERAGE=true` before running a test task to check coverage with Simplecov.
|
563
50
|
|
51
|
+
Github's pull requests and issues are used to communicate, send bug reports and code contributions.
|
52
|
+
|
564
53
|
## License
|
565
54
|
|
566
55
|
This software is licensed under the [Apache 2 license](./LICENSE).
|
data/Rakefile
CHANGED
@@ -110,6 +110,15 @@ namespace :docker do
|
|
110
110
|
end
|
111
111
|
end
|
112
112
|
|
113
|
+
desc 'Run Ruby console with the Elastic transport client libraries loaded'
|
114
|
+
task :console do
|
115
|
+
require 'irb'
|
116
|
+
require 'irb/completion'
|
117
|
+
require 'elastic-transport'
|
118
|
+
ARGV.clear
|
119
|
+
IRB.start
|
120
|
+
end
|
121
|
+
|
113
122
|
# ----- Documentation tasks ---------------------------------------------------
|
114
123
|
require 'yard'
|
115
124
|
YARD::Rake::YardocTask.new(:doc) do |t|
|
@@ -24,12 +24,12 @@ module Elastic
|
|
24
24
|
include Loggable
|
25
25
|
|
26
26
|
DEFAULT_PORT = 9200
|
27
|
-
DEFAULT_PROTOCOL = 'http'
|
27
|
+
DEFAULT_PROTOCOL = 'http'.freeze
|
28
28
|
DEFAULT_RELOAD_AFTER = 10_000 # Requests
|
29
29
|
DEFAULT_RESURRECT_AFTER = 60 # Seconds
|
30
30
|
DEFAULT_MAX_RETRIES = 3 # Requests
|
31
31
|
DEFAULT_SERIALIZER_CLASS = Serializer::MultiJson
|
32
|
-
SANITIZED_PASSWORD = '*' * (rand(14)+1)
|
32
|
+
SANITIZED_PASSWORD = '*' * (rand(14) + 1)
|
33
33
|
|
34
34
|
attr_reader :hosts, :options, :connections, :counter, :last_request_at, :protocol
|
35
35
|
attr_accessor :serializer, :sniffer, :logger, :tracer,
|
@@ -59,7 +59,7 @@ module Elastic
|
|
59
59
|
@compression = !!@options[:compression]
|
60
60
|
@connections = __build_connections
|
61
61
|
|
62
|
-
@serializer = options[:serializer] || ( options[:serializer_class] ? options[:serializer_class].new(self) : DEFAULT_SERIALIZER_CLASS.new(self)
|
62
|
+
@serializer = options[:serializer] || ( options[:serializer_class] ? options[:serializer_class].new(self) : DEFAULT_SERIALIZER_CLASS.new(self))
|
63
63
|
@protocol = options[:protocol] || DEFAULT_PROTOCOL
|
64
64
|
|
65
65
|
@logger = options[:logger]
|
@@ -72,7 +72,7 @@ module Elastic
|
|
72
72
|
@reload_connections = options[:reload_connections]
|
73
73
|
@reload_after = options[:reload_connections].is_a?(Integer) ? options[:reload_connections] : DEFAULT_RELOAD_AFTER
|
74
74
|
@resurrect_after = options[:resurrect_after] || DEFAULT_RESURRECT_AFTER
|
75
|
-
@retry_on_status = Array(options[:retry_on_status]).map
|
75
|
+
@retry_on_status = Array(options[:retry_on_status]).map(&:to_i)
|
76
76
|
end
|
77
77
|
|
78
78
|
# Returns a connection from the connection pool by delegating to {Connections::Collection#get_connection}.
|
@@ -87,7 +87,7 @@ module Elastic
|
|
87
87
|
resurrect_dead_connections! if Time.now > @last_request_at + @resurrect_after
|
88
88
|
|
89
89
|
@counter_mtx.synchronize { @counter += 1 }
|
90
|
-
reload_connections!
|
90
|
+
reload_connections! if reload_connections && (counter % reload_after).zero?
|
91
91
|
connections.get_connection(options)
|
92
92
|
end
|
93
93
|
|
@@ -97,10 +97,10 @@ module Elastic
|
|
97
97
|
#
|
98
98
|
def reload_connections!
|
99
99
|
hosts = sniffer.hosts
|
100
|
-
__rebuild_connections
|
100
|
+
__rebuild_connections(hosts: hosts, options: options)
|
101
101
|
self
|
102
102
|
rescue SnifferTimeoutError
|
103
|
-
log_error
|
103
|
+
log_error('[SnifferTimeoutError] Timeout when reloading connections.')
|
104
104
|
self
|
105
105
|
end
|
106
106
|
|
@@ -109,7 +109,7 @@ module Elastic
|
|
109
109
|
# @see Connections::Connection#resurrect!
|
110
110
|
#
|
111
111
|
def resurrect_dead_connections!
|
112
|
-
connections.dead.each
|
112
|
+
connections.dead.each(&:resurrect!)
|
113
113
|
end
|
114
114
|
|
115
115
|
# Rebuilds the connections collection in the transport.
|
@@ -128,7 +128,7 @@ module Elastic
|
|
128
128
|
__close_connections
|
129
129
|
|
130
130
|
new_connections = __build_connections
|
131
|
-
stale_connections = @connections.all.
|
131
|
+
stale_connections = @connections.all.reject { |c| new_connections.include?(c) }
|
132
132
|
new_connections = new_connections.reject { |c| @connections.all.include?(c) }
|
133
133
|
|
134
134
|
@connections.remove(stale_connections)
|
@@ -177,8 +177,8 @@ module Elastic
|
|
177
177
|
# @return [Connections::Connection]
|
178
178
|
# @api private
|
179
179
|
#
|
180
|
-
def __build_connection(host, options={}, block=nil)
|
181
|
-
raise NoMethodError,
|
180
|
+
def __build_connection(host, options = {}, block = nil)
|
181
|
+
raise NoMethodError, 'Implement this method in your class'
|
182
182
|
end
|
183
183
|
|
184
184
|
# Closes the connections collection
|
@@ -209,14 +209,14 @@ module Elastic
|
|
209
209
|
#
|
210
210
|
def __trace(method, path, params, headers, body, url, response, json, took, duration)
|
211
211
|
trace_url = "http://localhost:9200/#{path}?pretty" +
|
212
|
-
(
|
212
|
+
(params.empty? ? '' : "&#{::Faraday::Utils::ParamsHash[params].to_query}")
|
213
213
|
trace_body = body ? " -d '#{__convert_to_json(body, :pretty => true)}'" : ''
|
214
214
|
trace_command = "curl -X #{method.to_s.upcase}"
|
215
|
-
trace_command += " -H '#{headers.collect { |k,v| "#{k}: #{v}" }.join(", ")}'" if headers && !headers.empty?
|
215
|
+
trace_command += " -H '#{headers.collect { |k, v| "#{k}: #{v}" }.join(", ")}'" if headers && !headers.empty?
|
216
216
|
trace_command += " '#{trace_url}'#{trace_body}\n"
|
217
217
|
tracer.info trace_command
|
218
218
|
tracer.debug "# #{Time.now.iso8601} [#{response.status}] (#{format('%.3f', duration)}s)\n#"
|
219
|
-
tracer.debug json ? serializer.dump(json, :
|
219
|
+
tracer.debug json ? serializer.dump(json, pretty: true).gsub(/^/, '# ').sub(/\}$/, "\n# }")+"\n" : "# #{response.body}\n"
|
220
220
|
end
|
221
221
|
|
222
222
|
# Raise error specific for the HTTP response status or a generic server error
|
@@ -276,40 +276,37 @@ module Elastic
|
|
276
276
|
reload_on_failure = opts.fetch(:reload_on_failure, @options[:reload_on_failure])
|
277
277
|
delay_on_retry = opts.fetch(:delay_on_retry, @options[:delay_on_retry])
|
278
278
|
|
279
|
-
max_retries =
|
280
|
-
opts[:retry_on_failure] === true ? DEFAULT_MAX_RETRIES : opts[:retry_on_failure]
|
281
|
-
elsif options.key?(:retry_on_failure)
|
282
|
-
options[:retry_on_failure] === true ? DEFAULT_MAX_RETRIES : options[:retry_on_failure]
|
283
|
-
end
|
279
|
+
max_retries = max_retries(opts) || max_retries(options)
|
284
280
|
|
285
281
|
params = params.clone
|
286
|
-
|
282
|
+
# Transforms ignore status codes to Integer
|
283
|
+
ignore = Array(params.delete(:ignore)).compact.map(&:to_i)
|
287
284
|
|
288
285
|
begin
|
289
286
|
sleep(delay_on_retry / 1000.0) if tries > 0
|
290
|
-
tries
|
287
|
+
tries += 1
|
291
288
|
connection = get_connection or raise Error.new('Cannot get new connection from pool.')
|
292
289
|
|
293
|
-
if connection.connection.respond_to?(:params) &&
|
290
|
+
if connection.connection.respond_to?(:params) &&
|
291
|
+
connection.connection.params.respond_to?(:to_hash)
|
294
292
|
params = connection.connection.params.merge(params.to_hash)
|
295
293
|
end
|
296
294
|
|
297
|
-
url
|
295
|
+
url = connection.full_url(path, params)
|
298
296
|
response = block.call(connection, url)
|
299
|
-
connection.healthy! if connection.failures
|
297
|
+
connection.healthy! if connection.failures.positive?
|
300
298
|
|
301
299
|
# Raise an exception so we can catch it for `retry_on_status`
|
302
|
-
__raise_transport_error(response) if response.status.to_i >= 300 &&
|
300
|
+
__raise_transport_error(response) if response.status.to_i >= 300 &&
|
301
|
+
@retry_on_status.include?(response.status.to_i)
|
303
302
|
rescue Elastic::Transport::Transport::ServerError => e
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
log_fatal "[#{e.class}] Cannot get response from #{url} after #{tries} tries"
|
310
|
-
raise e
|
311
|
-
end
|
303
|
+
raise e unless response && @retry_on_status.include?(response.status)
|
304
|
+
|
305
|
+
log_warn "[#{e.class}] Attempt #{tries} to get response from #{url}"
|
306
|
+
if tries <= (max_retries || DEFAULT_MAX_RETRIES)
|
307
|
+
retry
|
312
308
|
else
|
309
|
+
log_fatal "[#{e.class}] Cannot get response from #{url} after #{tries} tries"
|
313
310
|
raise e
|
314
311
|
end
|
315
312
|
rescue *host_unreachable_exceptions => e
|
@@ -317,21 +314,21 @@ module Elastic
|
|
317
314
|
|
318
315
|
connection.dead!
|
319
316
|
|
320
|
-
if reload_on_failure
|
317
|
+
if reload_on_failure && tries < connections.all.size
|
321
318
|
log_warn "[#{e.class}] Reloading connections (attempt #{tries} of #{connections.all.size})"
|
322
319
|
reload_connections! and retry
|
323
320
|
end
|
324
321
|
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
end
|
322
|
+
exception = Elastic::Transport::Transport::Error.new(e.message)
|
323
|
+
|
324
|
+
raise exception unless max_retries
|
325
|
+
|
326
|
+
log_warn "[#{e.class}] Attempt #{tries} connecting to #{connection.host.inspect}"
|
327
|
+
if tries <= max_retries
|
328
|
+
retry
|
333
329
|
else
|
334
|
-
|
330
|
+
log_fatal "[#{e.class}] Cannot connect to #{connection.host.inspect} after #{tries} tries"
|
331
|
+
raise exception
|
335
332
|
end
|
336
333
|
rescue Exception => e
|
337
334
|
log_fatal "[#{e.class}] #{e.message} (#{connection.host.inspect if connection})"
|
@@ -349,8 +346,11 @@ module Elastic
|
|
349
346
|
__raise_transport_error response unless ignore.include?(response.status.to_i)
|
350
347
|
end
|
351
348
|
|
352
|
-
json
|
353
|
-
|
349
|
+
json = serializer.load(response.body) if response.body &&
|
350
|
+
!response.body.empty? &&
|
351
|
+
response.headers &&
|
352
|
+
response.headers["content-type"] =~ /json/
|
353
|
+
took = (json['took'] ? sprintf('%.3fs', json['took'] / 1000.0) : 'n/a') rescue 'n/a'
|
354
354
|
__log_response(method, path, params, body, url, response, json, took, duration) unless ignore.include?(response.status.to_i)
|
355
355
|
__trace(method, path, params, connection_headers(connection), body, url, response, nil, 'N/A', duration) if tracer
|
356
356
|
log_warn(response.headers['warning']) if response.headers&.[]('warning')
|
@@ -372,17 +372,21 @@ module Elastic
|
|
372
372
|
private
|
373
373
|
|
374
374
|
USER_AGENT_STR = 'User-Agent'.freeze
|
375
|
-
USER_AGENT_REGEX = /user
|
375
|
+
USER_AGENT_REGEX = /user-?_?agent/
|
376
376
|
ACCEPT_ENCODING = 'Accept-Encoding'.freeze
|
377
377
|
CONTENT_ENCODING = 'Content-Encoding'.freeze
|
378
378
|
CONTENT_TYPE_STR = 'Content-Type'.freeze
|
379
|
-
CONTENT_TYPE_REGEX = /content
|
379
|
+
CONTENT_TYPE_REGEX = /content-?_?type/
|
380
380
|
DEFAULT_CONTENT_TYPE = 'application/json'.freeze
|
381
381
|
GZIP = 'gzip'.freeze
|
382
382
|
GZIP_FIRST_TWO_BYTES = '1f8b'.freeze
|
383
383
|
HEX_STRING_DIRECTIVE = 'H*'.freeze
|
384
384
|
RUBY_ENCODING = '1.9'.respond_to?(:force_encoding)
|
385
385
|
|
386
|
+
def max_retries(opts)
|
387
|
+
opts[:retry_on_failure] == true ? DEFAULT_MAX_RETRIES : opts[:retry_on_failure]
|
388
|
+
end
|
389
|
+
|
386
390
|
def compress_request(body, headers)
|
387
391
|
if body
|
388
392
|
headers ||= {}
|
@@ -409,7 +413,7 @@ module Elastic
|
|
409
413
|
|
410
414
|
io = StringIO.new(body)
|
411
415
|
gzip_reader = if RUBY_ENCODING
|
412
|
-
Zlib::GzipReader.new(io, :
|
416
|
+
Zlib::GzipReader.new(io, encoding: 'ASCII-8BIT')
|
413
417
|
else
|
414
418
|
Zlib::GzipReader.new(io)
|
415
419
|
end
|
@@ -442,7 +446,7 @@ module Elastic
|
|
442
446
|
end
|
443
447
|
end
|
444
448
|
|
445
|
-
def user_agent_header(
|
449
|
+
def user_agent_header(_client)
|
446
450
|
@user_agent ||= begin
|
447
451
|
meta = ["RUBY_VERSION: #{RUBY_VERSION}"]
|
448
452
|
if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
|
@@ -453,7 +457,8 @@ module Elastic
|
|
453
457
|
end
|
454
458
|
|
455
459
|
def connection_headers(connection)
|
456
|
-
if defined?(Elastic::Transport::Transport::HTTP::Manticore) &&
|
460
|
+
if defined?(Elastic::Transport::Transport::HTTP::Manticore) &&
|
461
|
+
instance_of?(Elastic::Transport::Transport::HTTP::Manticore)
|
457
462
|
@request_options[:headers]
|
458
463
|
else
|
459
464
|
connection.connection.headers
|
@@ -18,7 +18,6 @@
|
|
18
18
|
module Elastic
|
19
19
|
module Transport
|
20
20
|
module Transport
|
21
|
-
|
22
21
|
# Generic client error
|
23
22
|
#
|
24
23
|
class Error < StandardError; end
|
@@ -78,14 +77,13 @@ module Elastic
|
|
78
77
|
505 => 'HTTPVersionNotSupported',
|
79
78
|
506 => 'VariantAlsoNegotiates',
|
80
79
|
510 => 'NotExtended'
|
81
|
-
}
|
80
|
+
}.freeze
|
82
81
|
|
83
|
-
ERRORS = HTTP_STATUSES.
|
82
|
+
ERRORS = HTTP_STATUSES.each_with_object({}) do |error, sum|
|
84
83
|
status, name = error
|
85
84
|
sum[status] = Errors.const_set name, Class.new(ServerError)
|
86
85
|
sum
|
87
86
|
end
|
88
|
-
|
89
87
|
end
|
90
88
|
end
|
91
89
|
end
|
@@ -34,26 +34,27 @@ module Elastic
|
|
34
34
|
#
|
35
35
|
def perform_request(method, path, params = {}, body = nil, headers = nil, opts = {})
|
36
36
|
super do |connection, url|
|
37
|
-
headers =
|
38
|
-
if !headers.nil?
|
39
|
-
connection.connection.headers.merge(headers)
|
40
|
-
else
|
41
|
-
connection.connection.headers
|
42
|
-
end
|
43
|
-
else
|
44
|
-
headers
|
45
|
-
end
|
37
|
+
headers = parse_headers(headers, connection)
|
46
38
|
body = body ? __convert_to_json(body) : nil
|
47
39
|
body, headers = compress_request(body, headers)
|
48
40
|
|
49
|
-
response = connection.connection.run_request(
|
50
|
-
method.downcase.to_sym,
|
51
|
-
url,
|
52
|
-
body,
|
53
|
-
headers
|
54
|
-
)
|
41
|
+
response = connection.connection.run_request(method.downcase.to_sym, url, body, headers)
|
55
42
|
|
56
|
-
Response.new
|
43
|
+
Response.new(response.status, decompress_response(response.body), response.headers)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Merges headers already present in the connection and the ones passed in to perform_request
|
48
|
+
#
|
49
|
+
def parse_headers(headers, connection)
|
50
|
+
if connection.connection.headers
|
51
|
+
if !headers.nil?
|
52
|
+
connection.connection.headers.merge(headers)
|
53
|
+
else
|
54
|
+
connection.connection.headers
|
55
|
+
end
|
56
|
+
else
|
57
|
+
headers
|
57
58
|
end
|
58
59
|
end
|
59
60
|
|
@@ -73,10 +74,10 @@ module Elastic
|
|
73
74
|
#
|
74
75
|
def host_unreachable_exceptions
|
75
76
|
[
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
77
|
+
::Faraday::ConnectionFailed,
|
78
|
+
::Faraday::TimeoutError,
|
79
|
+
::Faraday.const_defined?(:ServerError) ? ::Faraday::ServerError : nil,
|
80
|
+
::Faraday::SSLError
|
80
81
|
].compact
|
81
82
|
end
|
82
83
|
|
@@ -84,13 +85,14 @@ module Elastic
|
|
84
85
|
|
85
86
|
def user_agent_header(client)
|
86
87
|
@user_agent ||= begin
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
88
|
+
meta = ["RUBY_VERSION: #{RUBY_VERSION}"]
|
89
|
+
if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
|
90
|
+
meta << "#{RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase} " \
|
91
|
+
"#{RbConfig::CONFIG['target_cpu']}"
|
92
|
+
end
|
93
|
+
meta << client.headers[USER_AGENT_STR]
|
94
|
+
"elastic-transport-ruby/#{VERSION} (#{meta.join('; ')})"
|
95
|
+
end
|
94
96
|
end
|
95
97
|
end
|
96
98
|
end
|
@@ -19,14 +19,15 @@ module Elastic
|
|
19
19
|
module Transport
|
20
20
|
module Transport
|
21
21
|
# Wraps the response from Elasticsearch.
|
22
|
-
#
|
22
|
+
# It provides `body`, `status` and `headers` methods, but you can treat is as a hash and
|
23
|
+
# access the keys directly.
|
23
24
|
class Response
|
24
25
|
attr_reader :status, :body, :headers
|
25
26
|
|
26
27
|
# @param status [Integer] Response status code
|
27
28
|
# @param body [String] Response body
|
28
29
|
# @param headers [Hash] Response headers
|
29
|
-
def initialize(status, body, headers={})
|
30
|
+
def initialize(status, body, headers = {})
|
30
31
|
@status, @body, @headers = status, body, headers
|
31
32
|
@body = body.force_encoding('UTF-8') if body.respond_to?(:force_encoding)
|
32
33
|
end
|
@@ -30,16 +30,16 @@ describe Elastic::Transport::Transport::Base do
|
|
30
30
|
|
31
31
|
it 'does not include the password in the logged string' do
|
32
32
|
expect(logger).not_to receive(:error).with(/secret_password/)
|
33
|
-
expect
|
33
|
+
expect do
|
34
34
|
client.perform_request('GET', '/_cluster/stats')
|
35
|
-
|
35
|
+
end.to raise_exception(Elastic::Transport::Transport::Error)
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'replaces the password with the string \'REDACTED\'' do
|
39
39
|
expect(logger).to receive(:error).with(/REDACTED/)
|
40
|
-
expect
|
40
|
+
expect do
|
41
41
|
client.perform_request('GET', '/_cluster/stats')
|
42
|
-
|
42
|
+
end.to raise_exception(Elastic::Transport::Transport::Error)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -93,7 +93,7 @@ describe Elastic::Transport::Transport::Base do
|
|
93
93
|
end
|
94
94
|
|
95
95
|
it 'raises an exception' do
|
96
|
-
expect { client.perform_request('GET', '/info') }.to raise_exception(
|
96
|
+
expect { client.perform_request('GET', '/info') }.to raise_exception(Elastic::Transport::Transport::Error)
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -116,9 +116,9 @@ describe Elastic::Transport::Transport::Base do
|
|
116
116
|
end
|
117
117
|
|
118
118
|
it 'uses the client `retry_on_failure` value' do
|
119
|
-
expect
|
119
|
+
expect do
|
120
120
|
client.transport.perform_request('GET', '/info')
|
121
|
-
|
121
|
+
end.to raise_exception(Elastic::Transport::Transport::Error)
|
122
122
|
end
|
123
123
|
end
|
124
124
|
|
@@ -150,7 +150,7 @@ describe Elastic::Transport::Transport::Base do
|
|
150
150
|
it 'uses the option `retry_on_failure` value' do
|
151
151
|
expect do
|
152
152
|
client.transport.perform_request('GET', '/info', {}, nil, nil, retry_on_failure: 5)
|
153
|
-
end.to raise_exception(
|
153
|
+
end.to raise_exception(Elastic::Transport::Transport::Error)
|
154
154
|
end
|
155
155
|
end
|
156
156
|
end
|
@@ -162,8 +162,8 @@ describe Elastic::Transport::Transport::Base do
|
|
162
162
|
|
163
163
|
let(:arguments) do
|
164
164
|
{
|
165
|
-
|
166
|
-
|
165
|
+
hosts: ['http://unavailable:9200', 'http://unavailable:9201'],
|
166
|
+
retry_on_failure: true
|
167
167
|
}
|
168
168
|
end
|
169
169
|
|
@@ -173,9 +173,9 @@ describe Elastic::Transport::Transport::Base do
|
|
173
173
|
end
|
174
174
|
|
175
175
|
it 'uses the default `MAX_RETRIES` value' do
|
176
|
-
expect
|
176
|
+
expect do
|
177
177
|
client.transport.perform_request('GET', '/info')
|
178
|
-
|
178
|
+
end.to raise_exception(Elastic::Transport::Transport::Error)
|
179
179
|
end
|
180
180
|
end
|
181
181
|
|
@@ -185,9 +185,9 @@ describe Elastic::Transport::Transport::Base do
|
|
185
185
|
end
|
186
186
|
|
187
187
|
it 'uses the option `retry_on_failure` value' do
|
188
|
-
expect
|
188
|
+
expect do
|
189
189
|
client.transport.perform_request('GET', '/info', {}, nil, nil, retry_on_failure: 5)
|
190
|
-
|
190
|
+
end.to raise_exception(Elastic::Transport::Transport::Error)
|
191
191
|
end
|
192
192
|
end
|
193
193
|
end
|
@@ -199,8 +199,8 @@ describe Elastic::Transport::Transport::Base do
|
|
199
199
|
|
200
200
|
let(:arguments) do
|
201
201
|
{
|
202
|
-
|
203
|
-
|
202
|
+
hosts: ['http://unavailable:9200', 'http://unavailable:9201'],
|
203
|
+
retry_on_failure: false
|
204
204
|
}
|
205
205
|
end
|
206
206
|
|
@@ -210,22 +210,21 @@ describe Elastic::Transport::Transport::Base do
|
|
210
210
|
end
|
211
211
|
|
212
212
|
it 'does not retry' do
|
213
|
-
expect
|
213
|
+
expect do
|
214
214
|
client.transport.perform_request('GET', '/info')
|
215
|
-
|
215
|
+
end.to raise_exception(Elastic::Transport::Transport::Error)
|
216
216
|
end
|
217
217
|
end
|
218
218
|
|
219
219
|
context 'when `perform_request` is called with a `retry_on_failure` option value' do
|
220
|
-
|
221
220
|
before do
|
222
221
|
expect(client.transport).to receive(:get_connection).exactly(6).times.and_call_original
|
223
222
|
end
|
224
223
|
|
225
224
|
it 'uses the option `retry_on_failure` value' do
|
226
|
-
expect
|
225
|
+
expect do
|
227
226
|
client.transport.perform_request('GET', '/info', {}, nil, nil, retry_on_failure: 5)
|
228
|
-
|
227
|
+
end.to raise_exception(Elastic::Transport::Transport::Error)
|
229
228
|
end
|
230
229
|
end
|
231
230
|
end
|
@@ -247,7 +246,7 @@ describe Elastic::Transport::Transport::Base do
|
|
247
246
|
it 'does not retry' do
|
248
247
|
expect do
|
249
248
|
client.transport.perform_request('GET', '/info')
|
250
|
-
end.to raise_exception(
|
249
|
+
end.to raise_exception(Elastic::Transport::Transport::Error)
|
251
250
|
end
|
252
251
|
end
|
253
252
|
|
@@ -259,7 +258,7 @@ describe Elastic::Transport::Transport::Base do
|
|
259
258
|
it 'uses the option `retry_on_failure` value' do
|
260
259
|
expect do
|
261
260
|
client.transport.perform_request('GET', '/info', {}, nil, nil, retry_on_failure: 5)
|
262
|
-
end.to raise_exception(
|
261
|
+
end.to raise_exception(Elastic::Transport::Transport::Error)
|
263
262
|
end
|
264
263
|
end
|
265
264
|
end
|
@@ -1194,17 +1194,7 @@ describe Elastic::Transport::Client do
|
|
1194
1194
|
|
1195
1195
|
context 'when the client connects to Elasticsearch' do
|
1196
1196
|
let(:logger) do
|
1197
|
-
Logger.new(
|
1198
|
-
logger.formatter = proc do |severity, datetime, progname, msg|
|
1199
|
-
color = case severity
|
1200
|
-
when /INFO/ then :green
|
1201
|
-
when /ERROR|WARN|FATAL/ then :red
|
1202
|
-
when /DEBUG/ then :cyan
|
1203
|
-
else :white
|
1204
|
-
end
|
1205
|
-
ANSI.ansi(severity[0] + ' ', color, :faint) + ANSI.ansi(msg, :white, :faint) + "\n"
|
1206
|
-
end
|
1207
|
-
end unless ENV['QUIET']
|
1197
|
+
Logger.new($stderr) unless ENV['QUIET']
|
1208
1198
|
end
|
1209
1199
|
|
1210
1200
|
let(:port) do
|
@@ -1327,9 +1317,9 @@ describe Elastic::Transport::Client do
|
|
1327
1317
|
|
1328
1318
|
it 'retries only the specified number of times' do
|
1329
1319
|
expect(client.perform_request('GET', '_nodes/_local'))
|
1330
|
-
expect
|
1320
|
+
expect do
|
1331
1321
|
client.perform_request('GET', '_nodes/_local')
|
1332
|
-
|
1322
|
+
end.to raise_exception(Elastic::Transport::Transport::Error)
|
1333
1323
|
end
|
1334
1324
|
end
|
1335
1325
|
|
data/spec/spec_helper.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -255,10 +255,9 @@ class Elastic::Transport::Transport::BaseTest < Minitest::Test
|
|
255
255
|
should "raise an error on connection failure" do
|
256
256
|
@transport.expects(:get_connection).returns(stub_everything :failures => 1)
|
257
257
|
|
258
|
-
|
259
|
-
block = lambda { |a,b| raise ::Errno::ECONNREFUSED }
|
258
|
+
block = lambda { |a,b| raise Elastic::Transport::Transport::Error }
|
260
259
|
|
261
|
-
assert_raise ::
|
260
|
+
assert_raise Elastic::Transport::Transport::Error do
|
262
261
|
@transport.perform_request 'GET', '/', &block
|
263
262
|
end
|
264
263
|
end
|
@@ -291,11 +290,11 @@ class Elastic::Transport::Transport::BaseTest < Minitest::Test
|
|
291
290
|
c = stub_everything :failures => 1
|
292
291
|
@transport.expects(:get_connection).returns(c)
|
293
292
|
|
294
|
-
block = lambda { |a,b| raise
|
293
|
+
block = lambda { |a, b| raise Errno::ECONNREFUSED }
|
295
294
|
|
296
295
|
c.expects(:dead!)
|
297
296
|
|
298
|
-
assert_raise( ::
|
297
|
+
assert_raise( Elastic::Transport::Transport::Error ) { @transport.perform_request 'GET', '/', &block }
|
299
298
|
end
|
300
299
|
end
|
301
300
|
|
@@ -311,7 +310,7 @@ class Elastic::Transport::Transport::BaseTest < Minitest::Test
|
|
311
310
|
|
312
311
|
should "reload connections when host is unreachable" do
|
313
312
|
@block.expects(:call).times(2).
|
314
|
-
|
313
|
+
raises(Errno::ECONNREFUSED).
|
315
314
|
then.returns(stub_everything :failures => 1)
|
316
315
|
|
317
316
|
@transport.expects(:reload_connections!).returns([])
|
@@ -343,13 +342,13 @@ class Elastic::Transport::Transport::BaseTest < Minitest::Test
|
|
343
342
|
|
344
343
|
should "raise an error after max tries" do
|
345
344
|
@block.expects(:call).times(4).
|
346
|
-
|
345
|
+
raises(Errno::ECONNREFUSED).
|
347
346
|
then.raises(Errno::ECONNREFUSED).
|
348
347
|
then.raises(Errno::ECONNREFUSED).
|
349
348
|
then.raises(Errno::ECONNREFUSED).
|
350
349
|
then.returns(stub_everything :failures => 1)
|
351
350
|
|
352
|
-
assert_raise
|
351
|
+
assert_raise Elastic::Transport::Transport::Error do
|
353
352
|
@transport.perform_request('GET', '/', &@block)
|
354
353
|
end
|
355
354
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elastic-transport
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 8.
|
4
|
+
version: 8.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Karel Minarik
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2023-01-30 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: faraday
|