lib/bunny/session.rb in bunny-2.17.0 vs lib/bunny/session.rb in bunny-2.18.0

- old
+ new

@@ -122,10 +122,12 @@ # @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 (true) Should automatically recover from network failures? # @option connection_string_or_opts [Integer] :recovery_attempts (nil) Max number of recovery attempts, nil means forever # @option connection_string_or_opts [Integer] :reset_recovery_attempts_after_reconnection (true) Should recovery attempt counter be reset after successful reconnection? When set to false, the attempt counter will last through the entire lifetime of the connection object. + # @option connection_string_or_opts [Proc] :recovery_attempt_started (nil) Will be called before every connection recovery attempt + # @option connection_string_or_opts [Proc] :recovery_completed (nil) Will be called after successful connection recovery # @option connection_string_or_opts [Boolean] :recover_from_connection_close (true) Should this connection recover after receiving a server-sent connection.close (e.g. connection was force closed)? # @option connection_string_or_opts [Object] :session_error_handler (Thread.current) Object which responds to #raise that will act as a session error handler. Defaults to Thread.current, which will raise asynchronous exceptions in the thread that created the session. # # @option optz [String] :auth_mechanism ("PLAIN") Authentication mechanism, PLAIN or EXTERNAL # @option optz [String] :locale ("PLAIN") Locale RabbitMQ should use @@ -170,11 +172,11 @@ # should automatic recovery from network failures be used? @automatically_recover = if opts[:automatically_recover].nil? && opts[:automatic_recovery].nil? true else - opts[:automatically_recover] || opts[:automatic_recovery] + opts[:automatically_recover] | opts[:automatic_recovery] end @recovering_from_network_failure = false @max_recovery_attempts = opts[:recovery_attempts] @recovery_attempts = @max_recovery_attempts # When this is set, connection attempts won't be reset after @@ -216,12 +218,14 @@ @transport_mutex = @mutex_impl.new @status_mutex = @mutex_impl.new @address_index_mutex = @mutex_impl.new @channels = Hash.new - @recovery_completed = opts[:recovery_completed] + @recovery_attempt_started = opts[:recovery_attempt_started] + @recovery_completed = opts[:recovery_completed] + @session_error_handler = opts.fetch(:session_error_handler, Thread.current) self.reset_continuations self.initialize_transport @@ -531,11 +535,23 @@ ensure ch.close if ch.open? end end + # Defines a callable (e.g. a block) that will be called + # before every connection recovery attempt. + def before_recovery_attempt_starts(&block) + @recovery_attempt_started = block + end + # Defines a callable (e.g. a block) that will be called + # after successful connection recovery. + def after_recovery_completed(&block) + @recovery_completed = block + end + + # # Implementation # # @private @@ -748,10 +764,11 @@ # @private def recover_from_network_failure sleep @network_recovery_interval @logger.debug "Will attempt connection recovery..." + notify_of_recovery_attempt_start self.initialize_transport @logger.warn "Retrying connection on next host in line: #{@transport.host}:#{@transport.port}" self.start @@ -821,11 +838,12 @@ ch.recover_from_network_failure end end end - def after_recovery_completed(&block) - @recovery_completed = block + # @private + def notify_of_recovery_attempt_start + @recovery_attempt_started.call if @recovery_attempt_started end # @private def notify_of_recovery_completion @recovery_completed.call if @recovery_completed