Skip to content

Commit

Permalink
Merge pull request #280 from KazWolfe/ipv6-compat
Browse files Browse the repository at this point in the history
Allow connections to IPv6 addresses
  • Loading branch information
remeh authored Jul 21, 2023
2 parents 2103c3c + 0e55706 commit 02e217e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/datadog/statsd/udp_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def close
def connect
close if @socket

@socket = UDPSocket.new
family = Addrinfo.udp(host, port).afamily

@socket = UDPSocket.new(family)
@socket.connect(host, port)
end

Expand Down
28 changes: 28 additions & 0 deletions spec/statsd/udp_connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@
instance_double(Datadog::Statsd::Telemetry, sent: true, dropped_writer: true)
end

context 'using an IPv6 address' do
let(:host) { '2001:db8::dead:beef' }

it 'connects to an IPv6 host with the right address family' do
expect(UDPSocket)
.to receive(:new)
.with(Socket::AF_INET6)

subject.write('test')
end

it 'connects to the right host and port' do
expect(udp_socket)
.to receive(:connect)
.with('2001:db8::dead:beef', 4567)

subject.write('test')
end
end

it 'connects to an IPv4 host with the right address family' do
expect(UDPSocket)
.to receive(:new)
.with(Socket::AF_INET)

subject.write('test')
end

it 'connects to the right host and port' do
expect(udp_socket)
.to receive(:connect)
Expand Down

0 comments on commit 02e217e

Please sign in to comment.