Skip to content

Commit

Permalink
Handle ENOTCONN (DataDog#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
blaines committed Jan 23, 2019
1 parent e69f616 commit 81f80c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/datadog/statsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def write(message)
bad_socket = !@socket_path.nil? && (
boom.is_a?(Errno::ECONNREFUSED) ||
boom.is_a?(Errno::ECONNRESET) ||
boom.is_a?(Errno::ENOTCONN) ||
boom.is_a?(Errno::ENOENT)
)
if bad_socket
Expand All @@ -61,7 +62,8 @@ def write(message)

# Try once to reconnect if the socket has been closed
retries ||= 1
if retries <= 1 && boom.is_a?(IOError) && boom.message =~ /closed stream/i
if retries <= 1 && boom.is_a?(Errno::ENOTCONN) or
retries <= 1 && boom.is_a?(IOError) && boom.message =~ /closed stream/i
retries += 1
begin
@socket = connect
Expand Down
25 changes: 25 additions & 0 deletions spec/statsd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,31 @@ class Datadog::Statsd::SomeClass; end
end
end

describe "when socket throws not connected error" do
before do
@fake_socket = Minitest::Mock.new
@fake_socket.expect(:connect, true) { true }
@fake_socket.expect :sendmsg_nonblock, true, ['foo:1|c']
@fake_socket.expect(:sendmsg_nonblock, true) { raise Errno::ENOTCONN }

@fake_socket2 = Minitest::Mock.new
@fake_socket2.expect(:connect, true) { true }
@fake_socket2.expect :sendmsg_nonblock, true, ['bar:1|c']
end

it "should ignore message and try reconnect on next call" do
Socket.stub(:new, @fake_socket) do
@statsd.increment('foo')
end
@statsd.increment('baz')
Socket.stub(:new, @fake_socket2) do
@statsd.increment('bar')
end
@fake_socket.verify
@fake_socket2.verify
end
end

describe "when socket is full" do
before do
@fake_socket = Minitest::Mock.new
Expand Down

0 comments on commit 81f80c0

Please sign in to comment.