lib/bunny/session.rb in bunny-2.20.3 vs lib/bunny/session.rb in bunny-2.21.0
- old
+ new
@@ -69,10 +69,11 @@
DEFAULT_LOCALE = "en_GB"
# Default reconnection interval for TCP connection failures
DEFAULT_NETWORK_RECOVERY_INTERVAL = 5.0
+ DEFAULT_RECOVERABLE_EXCEPTIONS = [StandardError, TCPConnectionFailedForAllHosts, TCPConnectionFailed, AMQ::Protocol::EmptyResponseError, SystemCallError, Timeout::Error, Bunny::ConnectionLevelException, Bunny::ConnectionClosedError]
#
# API
#
@@ -89,10 +90,11 @@
# @return [Integer] Timeout for blocking protocol operations (queue.declare, queue.bind, etc), in milliseconds. Default is 15000.
attr_reader :continuation_timeout
attr_reader :network_recovery_interval
attr_reader :connection_name
attr_accessor :socket_configurator
+ attr_accessor :recoverable_exceptions
# @param [String, Hash] connection_string_or_opts Connection string or a hash of connection options
# @param [Hash] optz Extra options not related to connection
#
# @option connection_string_or_opts [String] :host ("127.0.0.1") Hostname or IP address to connect to
@@ -224,10 +226,12 @@
@recovery_attempt_started = opts[:recovery_attempt_started]
@recovery_completed = opts[:recovery_completed]
@session_error_handler = opts.fetch(:session_error_handler, Thread.current)
+ @recoverable_exceptions = DEFAULT_RECOVERABLE_EXCEPTIONS.dup
+
self.reset_continuations
self.initialize_transport
end
@@ -745,13 +749,11 @@
end
end
# @private
def recoverable_network_failure?(exception)
- # No reasonably smart strategy was suggested in a few years.
- # So just recover unconditionally. MK.
- true
+ @recoverable_exceptions.any? {|x| exception.kind_of? x}
end
# @private
def recovering_from_network_failure?
@recovering_from_network_failure
@@ -792,23 +794,26 @@
notify_of_recovery_completion
end
rescue HostListDepleted
reset_address_index
retry
- rescue TCPConnectionFailedForAllHosts, TCPConnectionFailed, AMQ::Protocol::EmptyResponseError, SystemCallError, Timeout::Error => e
- @logger.warn "TCP connection failed, reconnecting in #{@network_recovery_interval} seconds"
- if should_retry_recovery?
- decrement_recovery_attemp_counter!
- if recoverable_network_failure?(e)
+ rescue => e
+ if recoverable_network_failure?(e)
+ @logger.warn "TCP connection failed"
+ if should_retry_recovery?
+ @logger.warn "Reconnecting in #{@network_recovery_interval} seconds"
+ decrement_recovery_attemp_counter!
announce_network_failure_recovery
retry
+ else
+ @logger.error "Ran out of recovery attempts (limit set to #{@max_recovery_attempts}), giving up"
+ @transport.close
+ self.close(false)
+ @manually_closed = false
end
else
- @logger.error "Ran out of recovery attempts (limit set to #{@max_recovery_attempts}), giving up"
- @transport.close
- self.close(false)
- @manually_closed = false
+ raise e
end
end
# @private
def recovery_attempts_limited?
@@ -1354,10 +1359,10 @@
@transport.close rescue nil # Let's make sure the previous transport socket is closed
@transport = Transport.new(self,
host_from_address(address),
port_from_address(address),
@opts.merge(:session_error_handler => @session_error_handler)
- )
+ )
# Reset the cached progname for the logger only when no logger was provided
@default_logger.progname = self.to_s
@transport