Skip to content

Commit

Permalink
Add :recovery_attempts option to the connection
Browse files Browse the repository at this point in the history
`:recovery_attempts` options sets the maximum number of attempts Bunny
would make to recover from network failures before giving up and
throwing the original exception. `:recovery_attempts` is effective only
if `:automatically_recover` option is true.
  • Loading branch information
jafrog committed Jun 25, 2015
1 parent 5d7b669 commit fff7f44
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/bunny/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class Session
# @option connection_string_or_opts [IO, String] :log_file The file or path to use when creating a logger. Defaults to STDOUT.
# @option connection_string_or_opts [IO, String] :logfile DEPRECATED: use :log_file instead. The file or path to use when creating a logger. Defaults to STDOUT.
# @option connection_string_or_opts [Integer] :log_level The log level to use when creating a logger. Defaults to LOGGER::WARN
# @option connection_string_or_opts [Boolean] :automatically_recover Should automatically recover from network failures?
# @option connection_string_or_opts [Integer] :recovery_attempts Max number of recovery attempts
#
# @option optz [String] :auth_mechanism ("PLAIN") Authentication mechanism, PLAIN or EXTERNAL
# @option optz [String] :locale ("PLAIN") Locale RabbitMQ should use
Expand Down Expand Up @@ -152,6 +154,7 @@ def initialize(connection_string_or_opts = Hash.new, optz = Hash.new)
else
opts[:automatically_recover] || opts[:automatic_recovery]
end
@recovery_attempts = opts[:recovery_attempts]
@network_recovery_interval = opts.fetch(:network_recovery_interval, DEFAULT_NETWORK_RECOVERY_INTERVAL)
@recover_from_connection_close = opts.fetch(:recover_from_connection_close, false)
# in ms
Expand Down Expand Up @@ -661,10 +664,18 @@ def recover_from_network_failure
rescue TCPConnectionFailedForAllHosts, TCPConnectionFailed, AMQ::Protocol::EmptyResponseError => e
@logger.warn "TCP connection failed, reconnecting in #{@network_recovery_interval} seconds"
sleep @network_recovery_interval
retry if recoverable_network_failure?(e)
if should_retry_recovery?
@recovery_attempts -= 1 if @recovery_attempts
retry if recoverable_network_failure?(e)
end
end
end

# @private
def should_retry_recovery?
@recovery_attempts.nil? || @recovery_attempts > 0
end

# @private
def recover_channels
# default channel is reopened right after connection
Expand Down
19 changes: 19 additions & 0 deletions spec/higher_level_api/integration/connection_recovery_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ def with_open_multi_broken_host( c = Bunny.new( :hosts => ["broken", "127.0.0.1"
end
end

def with_recovery_attempts_limited_to(attempts = 3, &block)
c = Bunny.new(:recover_from_connection_close => true, :network_recovery_interval => 0.2, :recovery_attempts => attempts)
begin
c.start
block.call(c)
ensure
c.close
end
end

def ensure_queue_recovery(ch, q)
q.purge
x = ch.default_exchange
Expand Down Expand Up @@ -353,5 +363,14 @@ def ensure_exchange_binding_recovery(ch, source, destination, routing_key = "")
end
end
end

it "tries to recover for a given number of attempts" do
with_recovery_attempts_limited_to(1) do |c|
close_all_connections!
expect(c).to receive(:start).exactly(2).times.and_raise(Bunny::TCPConnectionFailed.new("test"))

wait_for_recovery
end
end
end
end

0 comments on commit fff7f44

Please sign in to comment.