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

RUBY-986 Fallback to IPv4 if IPv6 isn't supported #660

Merged
merged 1 commit into from
Jul 23, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/mongo/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ def to_s
def initialize_resolver!(timeout, ssl_options)
family = (host == 'localhost') ? ::Socket::AF_INET : ::Socket::AF_UNSPEC
error = nil
::Socket.getaddrinfo(host, nil, family, ::Socket::SOCK_STREAM).detect do |info|
::Socket.getaddrinfo(host, nil, family, ::Socket::SOCK_STREAM).each do |info|
begin
return FAMILY_MAP[info[4]].new(info[3], port, host).tap do |res|
res.socket(timeout, ssl_options).connect!
end
rescue IOError, SystemCallError => e
res = FAMILY_MAP[info[4]].new(info[3], port, host)
res.socket(timeout, ssl_options).connect!
return res
rescue IOError, SystemCallError, Error::SocketError => e
error = e
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/mongo/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,23 @@
end
end
end

describe "#socket" do
context 'when providing a DNS entry that resolves to both IPv6 and IPv4' do
let(:address) do
described_class.new('localhost:27017')
end

before do
allow(::Socket).to receive(:getaddrinfo).and_return(
[ ["AF_INET6", 0, "::1", "::1", ::Socket::AF_INET6, 1, 6],
["AF_INET", 0, "127.0.0.1", "127.0.0.1", ::Socket::AF_INET, 1, 6]]
)
end

it "attempts to use IPv6 and fallbacks to IPv4" do
expect(address.socket(0.0)).not_to be_nil
end
end
end
end