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

Support IP changes of statsd hostname #284

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions lib/datadog/statsd/udp_connection.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative 'connection'
require "resolv"

module Datadog
class Statsd
Expand Down Expand Up @@ -35,12 +36,30 @@ def connect
@socket.connect(host, port)
end

def check_dns_resolution
unless @current_host_ip && @last_dns_check
@current_host_ip = Resolv.getaddress(@host)
@last_dns_check = Time.now
end

return if Time.now - @last_dns_check < 60

@last_dns_check = Time.now
fresh_resolved_ip = Resolv.getaddress(@host)
return if @current_host_ip == fresh_resolved_ip

@current_host_ip = fresh_resolved_ip
close
connect
end

# send_message is writing the message in the socket, it may create the socket if nil
# It is not thread-safe but since it is called by either the Sender bg thread or the
# SingleThreadSender (which is using a mutex while Flushing), only one thread must call
# it at a time.
def send_message(message)
connect unless @socket
check_dns_resolution
@socket.send(message, 0)
end
end
Expand Down
58 changes: 58 additions & 0 deletions spec/integrations/connection_edge_case_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

let(:fake_socket) do
instance_double(UDPSocket,
close: true,
connect: true,
send: true)
end
Expand All @@ -31,6 +32,63 @@
send: true)
end

context 'when hostname resolves to a different ip address after connecting' do
it 'reconnects socket after 60 seconds if the ip changes' do
allow(Resolv).to receive(:getaddress)
.and_return("192.168.0.1", "192.168.0.2")

subject.write('foobar')
expect(fake_socket)
.to have_received(:send)
.with('foobar', anything)

subject.write('foobar')
expect(fake_socket)
.to have_received(:send)
.with('foobar', anything)
.twice

Timecop.travel(Time.now + 61) do
subject.write('foobar')
expect(fake_socket_retry)
.to have_received(:send)
.with('foobar', anything)
end

Timecop.travel(Time.now + 360) do
subject.write('foobar')
expect(fake_socket_retry)
.to have_received(:send)
.with('foobar', anything)
.twice
end
end

it 'does not reconnect socket after 60 seconds if the ip does not change' do
allow(Resolv).to receive(:getaddress)
.and_return("192.168.0.1")

subject.write('foobar')
expect(fake_socket)
.to have_received(:send)
.with('foobar', anything)

subject.write('foobar')
expect(fake_socket)
.to have_received(:send)
.with('foobar', anything)
.twice

Timecop.travel(Time.now + 61) do
subject.write('foobar')
expect(fake_socket)
.to have_received(:send)
.with('foobar', anything)
.exactly(3).times
end
end
end

context 'when having unknown SocketError (drop strategy)'do
before do
allow(fake_socket)
Expand Down