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

Commit

Permalink
Merge pull request #337 from mongoid/connection-ip-issues
Browse files Browse the repository at this point in the history
Connection ip issues
  • Loading branch information
durran committed Jan 2, 2015
2 parents 8831b8c + 71a8fa9 commit 84618a9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
guard(
"rspec",
all_after_pass: false,
cli: "--fail-fast --tty --format documentation --colour") do
cmd: "bundle exec rspec --fail-fast --tty --format documentation --colour") do

watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |match| "spec/#{match[1]}_spec.rb" }
Expand Down
3 changes: 2 additions & 1 deletion lib/moped/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def initialize(address, timeout)
# @since 2.0.0
def resolve(node)
begin
return @resolved if @resolved
Timeout::timeout(@timeout) do
Resolv.each_address(host) do |ip|
if ip =~ Resolv::IPv4::Regex
Expand All @@ -55,7 +56,7 @@ def resolve(node)
end
raise Resolv::ResolvError unless @ip
end
@resolved ||= "#{ip}:#{port}"
@resolved = "#{ip}:#{port}"
rescue Timeout::Error, Resolv::ResolvError, SocketError
Loggable.warn(" MOPED:", "Could not resolve IP for: #{original}", "n/a")
node.down! and false
Expand Down
20 changes: 20 additions & 0 deletions lib/moped/connection/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ def pool(node)
end
end

# Shutdown the connection pool for the provided node. In the case of
# unresolved IP addresses the resolved address would be nil resulting in
# the same pool for all nodes that did not have IP resolved.
#
# @example Shut down the connection pool.
# Manager.shutdown(node)
#
# @param [ Node ] node The node.
#
# @return [ nil ] Always nil.
#
# @since 2.0.3
def shutdown(node)
MUTEX.synchronize do
pool = pools.delete(node.address.resolved)
pool.shutdown{ |conn| conn.disconnect } if pool
nil
end
end

private

# Create a new connection pool for the provided node.
Expand Down
6 changes: 3 additions & 3 deletions lib/moped/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def down?
#
# @since 1.2.0
def disconnect
connection{ |conn| conn.disconnect } if address.resolved
connection{ |conn| conn.disconnect }
true
end

Expand All @@ -150,9 +150,10 @@ def disconnect
#
# @since 2.0.0
def down!
@pool = Connection::Manager.shutdown(self)
@down_at = Time.new
@latency = nil
disconnect
true
end

# Yields the block if a connection can be established, retrying when a
Expand Down Expand Up @@ -185,7 +186,6 @@ def ensure_connected(&block)
ensure
end_execution(:connection)
end

end

# Set a flag on the node for the duration of provided block so that an
Expand Down
12 changes: 12 additions & 0 deletions spec/moped/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@
end
end

pending '#down!' do

before do
node.connected?
node.down!
end

it 'clears out the connection pool' do
expect(node.instance_variable_get(:@pool)).to be_nil
end
end

describe "#latency" do

let(:node) do
Expand Down

0 comments on commit 84618a9

Please sign in to comment.