Skip to content

Commit d104ff8

Browse files
[DOCS] Revamps transport docs
Co-authored-by: István Zoltán Szabó <[email protected]>
1 parent 7653a9a commit d104ff8

File tree

1 file changed

+97
-35
lines changed

1 file changed

+97
-35
lines changed

docs/transport.asciidoc

Lines changed: 97 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,60 @@ When available, it handles connecting to multiple nodes in the cluster, rotating
77

88
It does not handle calling the {es} or Enterprise Search APIs.
99

10-
For optimal performance, use a HTTP library which supports persistent ("keep-alive") connections, such as https://github.com/toland/patron[patron] or https://github.com/typhoeus/typhoeus[Typhoeus]. Require the library (require 'patron') in your code, and it will be automatically used.
10+
This library uses https://github.com/lostisland/faraday[Faraday] by default as the HTTP transport implementation. We test it with Faraday versions 1.x and 2.x.
11+
12+
For optimal performance, use a HTTP library which supports persistent ("keep-alive") connections, such as https://github.com/toland/patron[patron] or https://github.com/typhoeus/typhoeus[Typhoeus]. 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.
13+
14+
Currently these libraries are supported:
15+
16+
* https://github.com/toland/patron[Patron]
17+
* https://github.com/typhoeus/typhoeus[Typhoeus]
18+
* https://rubygems.org/gems/httpclient[HTTPClient]
19+
* https://rubygems.org/gems/net-http-persistent[Net::HTTP::Persistent]
20+
21+
NOTE: Use https://github.com/typhoeus/typhoeus[Typhoeus] v1.4.0 or up since older versions are not compatible with Faraday 1.0.
22+
23+
You can customize Faraday and implement your own HTTP transport. For detailed information, see the example configurations and more information <<transport-implementations,below>>.
24+
25+
Features overview:
26+
27+
* Pluggable logging and tracing
28+
* Pluggable connection selection strategies (round-robin, random, custom)
29+
* Pluggable transport implementation, customizable and extendable
30+
* Pluggable serializer implementation
31+
* Request retries and dead connections handling
32+
* Node reloading (based on cluster state) on errors or on demand
33+
34+
Refer to <<advanced-config,Advanced Configuration>> to read about more configuration options.
1135

1236
[discrete]
1337
[[transport-install]]
1438
==== Installation
1539

1640
Install the package from https://rubygems.org/[Rubygems]:
1741

18-
```
42+
[source,bash]
43+
----------------------------
1944
gem install elastic-transport
20-
```
45+
----------------------------
2146

2247
To use an unreleased version, either add it to your `Gemfile` for
2348
http://gembundler.com/[Bundler]:
2449

25-
```
50+
[source,bash]
51+
----------------------------
2652
gem 'elastic-transport', git: '[email protected]:elastic/elastic-transport-ruby.git'
27-
```
53+
----------------------------
2854

2955
or install it from a source code checkout:
3056

31-
```
57+
[source,bash]
58+
----------------------------
3259
git clone https://github.com/elastic/elastic-transport-ruby.git
3360
cd elastic-transport
3461
bundle install
3562
rake install
36-
```
37-
63+
----------------------------
3864

3965
[discrete]
4066
[[transport-example-usage]]
@@ -51,25 +77,41 @@ response = client.perform_request('GET', '_cluster/health')
5177
# => #<Elastic::Transport::Transport::Response:0x007fc5d506ce38 @status=200, @body={ ... } >
5278
----------------------------
5379

54-
Full documentation is available at http://rubydoc.info/gems/elastic-transport.
80+
Documentation is included as RDoc annotations in the source code and available online at http://rubydoc.info/gems/elastic-transport[RubyDoc].
5581

5682
[discrete]
5783
[[transport-implementations]]
5884
==== Transport implementations
5985

6086
By default, the client uses the https://rubygems.org/gems/faraday[Faraday] HTTP library as a transport implementation.
6187

62-
It auto-detects and uses an adapter for Faraday based on gems loaded in your code, preferring HTTP clients with support for persistent connections.
88+
The Client auto-detects and uses 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 (https://github.com/lostisland/faraday/blob/main/UPGRADING.md#adapters-have-moved[read more here]). If you're using Faraday 1.x, you can require the HTTP library. To use the https://github.com/toland/patron[_Patron_] HTTP, for example, require it:
89+
6390

6491
To use the https://github.com/toland/patron[Patron] HTTP, for example, require it:
6592

66-
```
93+
[source,rb]
94+
----------------------------
6795
require 'patron'
68-
```
96+
----------------------------
97+
98+
If you're using Faraday 2.x, you need to add the corresponding adapter gem to your Gemfile and require it after you require `faraday`:
99+
100+
[source,rb]
101+
----------------------------
102+
# Gemfile
103+
gem 'faraday-patron'
104+
105+
# Code
106+
require 'faraday'
107+
require 'faraday/patron'
108+
----------------------------
109+
69110

70111
Then, create a new client, and the Patron gem will be used as the "driver":
71112

72-
```ruby
113+
[source,rb]
114+
----------------------------
73115
client = Elastic::Transport::Client.new
74116
75117
client.transport.connections.first.connection.builder.adapter
@@ -85,22 +127,43 @@ end
85127
# => Stiletoo : 24
86128
# => Stiletoo : 24
87129
# => ...
88-
```
130+
----------------------------
89131

90132
To use a specific adapter for Faraday, pass it as the `adapter` argument:
91133

92-
```ruby
134+
[source,rb]
135+
----------------------------
93136
client = Elastic::Client.new(adapter: :net_http_persistent)
94137
95138
client.transport.connections.first.connection.builder.handlers
96139
# => [Faraday::Adapter::NetHttpPersistent]
97-
```
140+
----------------------------
141+
142+
If you see this error:
143+
144+
[source,rb]
145+
----------------------------
146+
Faraday::Error: :net_http_persistent is not registered on Faraday::Adapter
147+
----------------------------
148+
When you're using Faraday 2, you need to require the adapter before instantiating the client:
149+
150+
[source,rb]
151+
----------------------------
152+
> client = Elasticsearch::Client.new(adapter: :net_http_persistent)
153+
Faraday::Error: :net_http_persistent is not registered on Faraday::Adapter
154+
> require 'faraday/net_http_persistent'
155+
=> true
156+
> client = Elasticsearch::Client.new(adapter: :net_http_persistent)
157+
=> #<Elasticsearch::Client:0x00007eff2e7728e0
158+
----------------------------
159+
98160

99161
When using the Elasticsearch or Enterprise Search clients, you can pass the `adapter` parameter when initializing the clients.
100162

101163
To pass options to the https://github.com/lostisland/faraday/blob/master/lib/faraday/connection.rb[`Faraday::Connection`] constructor, use the `transport_options` key:
102164

103-
```ruby
165+
[source,rb]
166+
----------------------------
104167
client = Elastic::Client.new(
105168
transport_options: {
106169
request: { open_timeout: 1 },
@@ -109,24 +172,26 @@ client = Elastic::Client.new(
109172
ssl: { verify: false }
110173
}
111174
)
112-
```
175+
----------------------------
113176

114177
To configure the Faraday instance directly, use a block:
115178

116-
```ruby
179+
[source,rb]
180+
----------------------------
117181
require 'patron'
118182
119183
client = Elastic::Client.new(host: 'localhost', port: '9200') do |f|
120184
f.response :logger
121185
f.adapter :patron
122186
end
123-
```
187+
----------------------------
124188

125189
You can use any standard Faraday middleware and plugins in the configuration block.
126190

127191
You can also initialize the transport class yourself, and pass it to the client constructor as the `transport` argument. The Elasticsearch and Enterprise Search clients accept `:transport` as parameter when initializing a client. So you can pass in a transport you've initialized with the following options:
128192

129-
```ruby
193+
[source,rb]
194+
----------------------------
130195
require 'patron'
131196
132197
transport_configuration = lambda do |f|
@@ -142,11 +207,12 @@ transport = Elastic::Transport::Transport::HTTP::Faraday.new(
142207
# Pass the transport to the client
143208
#
144209
client = Elastic::Client.new(transport: transport)
145-
```
210+
----------------------------
146211

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

149-
```ruby
214+
[source,rb]
215+
----------------------------
150216
# Set up the transport
151217
#
152218
faraday_configuration = lambda do |f|
@@ -174,30 +240,32 @@ client = Elastic::Client.new
174240
# Inject the transport to the client
175241
#
176242
client.transport = faraday_client
177-
```
243+
----------------------------
178244

179245
You can also use a bundled https://rubygems.org/gems/curb[Curb] based transport implementation:
180246

181-
```ruby
247+
[source,rb]
248+
----------------------------
182249
require 'curb'
183250
require 'elastic/transport/transport/http/curb'
184251
185252
client = Elastic::Client.new(transport_class: Elastic::Transport::Transport::HTTP::Curb)
186253
187254
client.transport.connections.first.connection
188255
# => #<Curl::Easy http://localhost:9200/>
189-
```
256+
----------------------------
190257

191258
It's possible to customize the Curb instance by passing a block to the constructor as well (in this case, as an inline block):
192259

193-
```ruby
260+
[source,rb]
261+
----------------------------
194262
transport = Elastic::Transport::Transport::HTTP::Curb.new(
195263
hosts: [ { host: 'localhost', port: '9200' } ],
196264
& lambda { |c| c.verbose = true }
197265
)
198266
199267
client = Elastic::Client.new(transport: transport)
200-
```
268+
----------------------------
201269

202270
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 by injecting it directly.
203271

@@ -206,17 +274,11 @@ You can write your own transport implementation by including the {Elastic::Trans
206274
==== Transport architecture
207275

208276
* `Elastic::Transport::Client` is composed of `Elastic::Transport::Transport`.
209-
210277
* `Elastic::Transport::Transport` is composed of `Elastic::Transport::Transport::Connections`, and an instance of logger, tracer, serializer and sniffer.
211-
212278
* Logger and tracer can be any object conforming to Ruby logging interface, for example, an instance of https://ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html[`Logger`], https://rubygems.org/gems/log4r[log4r], https://github.com/TwP/logging/[logging], and so on.
213-
214279
* The `Elastic::Transport::Transport::Serializer::Base` implementations handle converting data for {es} (for example, to JSON). You can implement your own serializer.
215-
216280
* `Elastic::Transport::Transport::Sniffer` allows to discover nodes in the cluster and use them as connections.
217-
218281
* `Elastic::Transport::Transport::Connections::Collection` is composed of `Elastic::Transport::Transport::Connections::Connection` instances and a selector instance.
219-
220282
* `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.
221-
222283
* The `Elastic::Transport::Transport::Connections::Selector::Base` implementations allow to choose connections from the pool, for example, in a round-robin or random fashion. You can implement your own selector strategy.
284+
* The `Elastic::Transport::Transport::Response` object wraps the Elasticsearch JSON response. It provides `body`, `status`, and `headers` methods but you can treat it as a hash and access the keys directly.

0 commit comments

Comments
 (0)