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

Handle ENOTCONN #102

Merged
merged 3 commits into from
Jan 18, 2019
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
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) ||

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might actually be better to not add this and instead relax the retry conditions on this line

if retries <= 1 && boom.is_a?(IOError) && boom.message =~ /closed stream/i

to just

        if retries <= 1

In our case as well as I'm sure in many others, we'd much rather immediately retry establishing the socket and resending the message rather than losing the message and having to wait for the next one for the socket to re-establish itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I changed the logic to retry the same message upon 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