Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Merge 9737286 into 1d96fc7
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurnn committed Sep 27, 2013
2 parents 1d96fc7 + 9737286 commit f8c957b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/moped.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "stringio"
require "monitor"
require "timeout"
require 'resolv'
require "bson"
require "optionable"
require "moped/errors"
Expand Down
17 changes: 13 additions & 4 deletions lib/moped/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ class Address
# @param [ String ] address The host:port pair as a string.
#
# @since 2.0.0
def initialize(address)
def initialize(address, timeout)
@original = address
@host, port = address.split(":")
@port = (port || 27017).to_i
@timeout = timeout
end

# Resolve the address for the provided node. If the address cannot be
Expand All @@ -45,10 +46,18 @@ def initialize(address)
# @since 2.0.0
def resolve(node)
begin
@ip ||= Socket.getaddrinfo(host, nil, Socket::AF_INET, Socket::SOCK_STREAM).first[3]
Timeout::timeout(@timeout) do
Resolv.each_address(host) do |ip|
if ip =~ Resolv::IPv4::Regex
@ip ||= ip
break
end
end
raise Resolv::ResolvError unless @ip
end
@resolved ||= "#{ip}:#{port}"
rescue SocketError => e
node.instrument(Node::WARN, prefix: " MOPED:", message: "Could not resolve IP for: #{original}")
rescue Timeout::Error, Resolv::ResolvError
Loggable.warn(" MOPED:", "Could not resolve IP for: #{original}", "n/a")
node.down! and false
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/moped/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ def hash
#
# @since 1.0.0
def initialize(address, options = {})
@address = Address.new(address)
@options = options
@down_at = nil
@refreshed_at = nil
@latency = nil
@primary = nil
@secondary = nil
@instrumenter = options[:instrumenter] || Instrumentable::Log
@address = Address.new(address, timeout)
@address.resolve(self)
end

Expand Down
10 changes: 5 additions & 5 deletions spec/moped/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
context "when a port is provided" do

let(:address) do
described_class.new("127.0.0.1:27017")
described_class.new("127.0.0.1:27017", 2)
end

it "sets the original address" do
Expand All @@ -26,7 +26,7 @@
context "when no port is provided" do

let(:address) do
described_class.new("localhost")
described_class.new("localhost", 2)
end

it "sets the original address" do
Expand All @@ -52,7 +52,7 @@
end

let(:address) do
described_class.new("127.0.0.1:27017")
described_class.new("127.0.0.1:27017", 2)
end

before do
Expand All @@ -75,7 +75,7 @@
end

let(:address) do
described_class.new("localhost:27017")
described_class.new("localhost:27017", 2)
end

before do
Expand All @@ -98,7 +98,7 @@
end

let(:address) do
described_class.new("notahost:27017")
described_class.new("notahost:27017", 1)
end

let!(:resolved) do
Expand Down

0 comments on commit f8c957b

Please sign in to comment.