Skip to content

Commit a9d114b

Browse files
DinoPullerUqidopicandocodigo
authored andcommitted
[Client] Adds delay_on_retry to wait between each failed connection
1 parent 5699cc2 commit a9d114b

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

elasticsearch-transport/lib/elasticsearch/transport/client.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class Client
9292
#
9393
# @option arguments [Boolean,Number] :retry_on_failure Retry X times when request fails before raising and
9494
# exception (false by default)
95+
# @option arguments [Number] :delay_on_retry Delay in milliseconds between each retry (0 by default)
96+
#
9597
# @option arguments Array<Number> :retry_on_status Retry when specific status codes are returned
9698
#
9799
# @option arguments [Boolean] :reload_on_failure Reload connections after failure (false by default)
@@ -137,6 +139,7 @@ def initialize(arguments={}, &block)
137139
@arguments[:tracer] ||= @arguments[:trace] ? DEFAULT_TRACER.call() : nil
138140
@arguments[:reload_connections] ||= false
139141
@arguments[:retry_on_failure] ||= false
142+
@arguments[:delay_on_retry] ||= 0
140143
@arguments[:reload_on_failure] ||= false
141144
@arguments[:randomize_hosts] ||= false
142145
@arguments[:transport_options] ||= {}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def initialize(arguments = {}, &block)
5454
@options = arguments[:options] || {}
5555
@options[:http] ||= {}
5656
@options[:retry_on_status] ||= []
57+
@options[:delay_on_retry] ||= 0
5758

5859
@block = block
5960
@compression = !!@options[:compression]
@@ -264,6 +265,7 @@ def perform_request(method, path, params = {}, body = nil, headers = nil, opts =
264265
start = Time.now
265266
tries = 0
266267
reload_on_failure = opts.fetch(:reload_on_failure, @options[:reload_on_failure])
268+
delay_on_retry = opts.fetch(:delay_on_retry, @options[:delay_on_retry])
267269

268270
max_retries = if opts.key?(:retry_on_failure)
269271
opts[:retry_on_failure] === true ? DEFAULT_MAX_RETRIES : opts[:retry_on_failure]
@@ -275,6 +277,7 @@ def perform_request(method, path, params = {}, body = nil, headers = nil, opts =
275277
ignore = Array(params.delete(:ignore)).compact.map { |s| s.to_i }
276278

277279
begin
280+
sleep(delay_on_retry / 1000.0) if tries > 0
278281
tries += 1
279282
connection = get_connection or raise Error.new('Cannot get new connection from pool.')
280283

@@ -306,7 +309,6 @@ def perform_request(method, path, params = {}, body = nil, headers = nil, opts =
306309
log_error "[#{e.class}] #{e.message} #{connection.host.inspect}"
307310

308311
connection.dead!
309-
310312
if reload_on_failure and tries < connections.all.size
311313
log_warn "[#{e.class}] Reloading connections (attempt #{tries} of #{connections.all.size})"
312314
reload_connections! and retry

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,29 @@
16591659
end
16601660
end
16611661

1662+
context 'when retry_on_failure is true and delay_on_retry is specified' do
1663+
context 'when a node is unreachable' do
1664+
let(:hosts) do
1665+
[ELASTICSEARCH_HOSTS.first, "foobar1", "foobar2"]
1666+
end
1667+
1668+
let(:options) do
1669+
{ retry_on_failure: true, delay_on_retry: 3000 }
1670+
end
1671+
1672+
let(:responses) do
1673+
5.times.collect do
1674+
client.perform_request('GET', '_nodes/_local')
1675+
end
1676+
end
1677+
1678+
it 'retries on failure' do
1679+
allow_any_instance_of(Object).to receive(:sleep).with(3000 / 1000)
1680+
expect(responses.all? { true }).to be(true)
1681+
end
1682+
end
1683+
end
1684+
16621685
context 'when reload_on_failure is true' do
16631686

16641687
let(:hosts) do

0 commit comments

Comments
 (0)