From f60c029a529c05fa74e2d37357af07282c7cdcaa Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 19 Sep 2013 14:47:49 -0400 Subject: [PATCH] force ipv4 connection for hosts that use ipv6 --- lib/instrumental/agent.rb | 17 ++++++++++++++++- spec/agent_spec.rb | 4 ++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/instrumental/agent.rb b/lib/instrumental/agent.rb index 1ccbc2c..d1f0a04 100644 --- a/lib/instrumental/agent.rb +++ b/lib/instrumental/agent.rb @@ -238,6 +238,16 @@ def report_exception(e) logger.error "Exception occurred: #{e.message}\n#{e.backtrace.join("\n")}" end + def ipv4_address_for_host(host, port) + addresses = Socket.getaddrinfo(host, port, 'AF_INET') + if (result = addresses.first) + _, _, address, _ = result + address + else + raise Exception.new("Couldn't get address information for host #{host}") + end + end + def send_command(cmd, *args) cmd = "%s %s\n" % [cmd, args.collect { |a| a.to_s }.join(" ")] if enabled? @@ -312,11 +322,16 @@ def send_with_reply_timeout(message) end end + + def run_worker_loop command_and_args = nil command_options = nil logger.info "connecting to collector" - @socket = with_timeout(CONNECT_TIMEOUT) { TCPSocket.new(host, port) } + @socket = Socket.new(Socket::PF_INET, Socket::SOCK_STREAM) + with_timeout(CONNECT_TIMEOUT) do + @socket.connect Socket.pack_sockaddr_in(port, ipv4_address_for_host(host, port)) + end logger.info "connected to collector at #{host}:#{port}" send_with_reply_timeout "hello version #{Instrumental::VERSION} hostname #{Socket.gethostname} pid #{Process.pid}" send_with_reply_timeout "authenticate #{@api_key}" diff --git a/spec/agent_spec.rb b/spec/agent_spec.rb index 3d627b2..b772bb9 100644 --- a/spec/agent_spec.rb +++ b/spec/agent_spec.rb @@ -383,7 +383,7 @@ def wait it "should not wait longer than EXIT_FLUSH_TIMEOUT seconds to exit a process" do @server = TestServer.new @agent = Instrumental::Agent.new('test_token', :collector => @server.host_and_port, :synchronous => false) - TCPSocket.stub!(:new) { |*args| sleep(5) && StringIO.new } + Socket.stub!(:new) { |*args| sleep(5) && StringIO.new } with_constants('Instrumental::Agent::EXIT_FLUSH_TIMEOUT' => 3) do if (pid = fork { @agent.increment('foo', 1) }) tm = Time.now.to_f @@ -398,7 +398,7 @@ def wait it "should not wait to exit a process if there are no commands queued" do @server = TestServer.new @agent = Instrumental::Agent.new('test_token', :collector => @server.host_and_port, :synchronous => false) - TCPSocket.stub!(:new) { |*args| sleep(5) && StringIO.new } + Socket.stub!(:new) { |*args| sleep(5) && StringIO.new } with_constants('Instrumental::Agent::EXIT_FLUSH_TIMEOUT' => 3) do if (pid = fork { @agent.increment('foo', 1); @agent.queue.clear }) tm = Time.now.to_f