Skip to content

Commit

Permalink
force ipv4 connection for hosts that use ipv6
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris committed Sep 19, 2013
1 parent 876ec32 commit f60c029
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 16 additions & 1 deletion lib/instrumental/agent.rb
Expand Up @@ -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?
Expand Down Expand Up @@ -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}"
Expand Down
4 changes: 2 additions & 2 deletions spec/agent_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f60c029

Please sign in to comment.