Skip to content

Commit

Permalink
Remove connections that error with Beaneater::NotConnected error from…
Browse files Browse the repository at this point in the history
… the pool
  • Loading branch information
avgerin0s committed Feb 12, 2015
1 parent 487e486 commit 7fd69f6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/beaneater/pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,17 @@ def safe_transmit(grace_period=1, &block)
retries = 1
begin
yield
rescue DrainingError, EOFError, Errno::ECONNRESET, Errno::EPIPE => ex
# TODO remove faulty connections from pool?
# https://github.com/kr/beanstalk-client-ruby/blob/master/lib/beanstalk-client/connection.rb#L405-410
rescue NotConnected, DrainingError, EOFError, Errno::ECONNRESET, Errno::EPIPE => ex
if retries < MAX_RETRIES
retries += 1
sleep(grace_period) if grace_period
retry
else # finished retrying, fail out
if ex.is_a?(NotConnected)
connection = @connections.delete(ex.connection)
connection.close
end

ex.is_a?(DrainingError) ? raise(ex) : raise(NotConnected, "Could not connect!")
end
end
Expand All @@ -165,4 +168,4 @@ def host_from_env
end

end # Pool
end # Beaneater
end # Beaneater
12 changes: 12 additions & 0 deletions test/pool_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@
assert_equal 'INSERTED', res[:status]
end

it "removes the connection with Beaneater::NotConnected" do
first_connection = @bp.connections.first
connections_count = @bp.connections.count
first_connection.stubs(:transmit).
raises(Beaneater::NotConnected.new(first_connection))

assert_raises(Beaneater::NotConnected) do
@bp.transmit_to_all "puts 0 0 10 2 \r\nxy"
end
assert_equal @bp.connections.count, connections_count - 1
end

it "should retry 3 times for temporary failed connection with EOFError" do
TCPSocket.any_instance.expects(:write).times(3)
TCPSocket.any_instance.expects(:readline).raises(EOFError).then.
Expand Down

0 comments on commit 7fd69f6

Please sign in to comment.