Skip to content

Commit

Permalink
Failed heartbeats will longer raise exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Bonebrake committed Aug 23, 2018
1 parent cf2b1a4 commit 95a59d3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/bunny/heartbeat_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def beat

if now > (@last_activity_time + @interval)
@logger.debug { "Sending a heartbeat, last activity time: #{@last_activity_time}, interval (s): #{@interval}" }
@transport.write_without_timeout(AMQ::Protocol::HeartbeatFrame.encode)
@transport.write_without_timeout(AMQ::Protocol::HeartbeatFrame.encode, true)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/bunny/transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,13 @@ def write(data)
end

# Writes data to the socket without timeout checks
def write_without_timeout(data)
def write_without_timeout(data, raise_exceptions = false)
begin
@writes_mutex.synchronize { @socket.write(data) }
@socket.flush
rescue SystemCallError, Bunny::ConnectionError, IOError => e
close
raise e if raise_exceptions

if @session.automatically_recover?
@session.handle_network_failure(e)
Expand Down
32 changes: 24 additions & 8 deletions spec/higher_level_api/integration/toxiproxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
describe Bunny::Channel, "#basic_publish" do
include RabbitMQ::Toxiproxy

before(:all) do
setup_toxiproxy
@connection = Bunny.new(:user => "bunny_gem", :password => "bunny_password", :vhost => "bunny_testbed",
host: "localhost:11111", heartbeat_timeout: 1)
@connection.start
end

after :all do
after :each do
@connection.close if @connection.open?
end

context "when the the connection detects missed heartbeats" do
context "when the the connection detects missed heartbeat with automatic recovery" do
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)
@connection.start
end

let(:queue_name) { "bunny.basic.publish.queue#{rand}" }

it "raises a ConnectionClosedError" do
Expand All @@ -30,6 +31,21 @@
cleanup_toxiproxy
end
end

context "when the the connection detects missed heartbeats without automatic recovery" do
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: false, threaded: false)
@connection.start
end

it "does not raise an exception on session thread" do
rabbitmq_toxiproxy.down do
sleep 5
end
end
end
end
end
else
Expand Down

0 comments on commit 95a59d3

Please sign in to comment.