Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce recovery_failed callback #666

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/bunny/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Session
# @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 [Proc] :recovery_attempts_exhausted (nil) Will be called when the connection recovery failed after the specified amount of recovery attempts
# @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.
#
Expand Down Expand Up @@ -225,6 +226,7 @@ def initialize(connection_string_or_opts = ENV['RABBITMQ_URL'], optz = Hash.new)

@recovery_attempt_started = opts[:recovery_attempt_started]
@recovery_completed = opts[:recovery_completed]
@recovery_attempts_exhausted = opts[:recovery_attempts_exhausted]

@session_error_handler = opts.fetch(:session_error_handler, Thread.current)

Expand Down Expand Up @@ -553,6 +555,12 @@ def after_recovery_completed(&block)
@recovery_completed = block
end

# Defines a callable (e.g. a block) that will be called
# when the connection recovery failed after the specified
# numbers of recovery attempts.
def after_recovery_attempts_exhausted(&block)
@recovery_attempts_exhausted = block
end

#
# Implementation
Expand Down Expand Up @@ -809,6 +817,7 @@ def recover_from_network_failure
@transport.close
self.close(false)
@manually_closed = false
notify_of_recovery_attempts_exhausted
end
else
raise e
Expand Down Expand Up @@ -859,6 +868,11 @@ def notify_of_recovery_completion
@recovery_completed.call if @recovery_completed
end

# @private
def notify_of_recovery_attempts_exhausted
@recovery_attempts_exhausted.call if @recovery_attempts_exhausted
end

# @private
def instantiate_connection_level_exception(frame)
case frame
Expand Down
5 changes: 4 additions & 1 deletion spec/higher_level_api/integration/toxiproxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@
end

context "recovery attempt limit that's exceeded" do
let(:dummy) { double(call: true) }

before(:each) do
setup_toxiproxy
@connection = Bunny.new(user: "bunny_gem", password: "bunny_password", vhost: "bunny_testbed",
host: "localhost:11111", heartbeat_timeout: 1, automatically_recover: true, network_recovery_interval: 1,
recovery_attempts: 2, reset_recovery_attempts_after_reconnection: true,
recovery_attempts: 2, recovery_attempts_exhausted: Proc.new { dummy.call }, reset_recovery_attempts_after_reconnection: true,
disconnect_timeout: 1)
@connection.start
end
Expand All @@ -68,6 +70,7 @@

expect(@connection.open?).to be(false)
expect(@connection.closed?).to be(true)
expect(dummy).to have_received(:call).once
end
end # context
end # describe
Expand Down