diff --git a/lib/moped/authenticatable.rb b/lib/moped/authenticatable.rb index 06d4ac3..de99a7f 100644 --- a/lib/moped/authenticatable.rb +++ b/lib/moped/authenticatable.rb @@ -28,6 +28,7 @@ def apply_credentials(logins) login(database, username, password) end end + @original_credentials = credentials.dup end self end diff --git a/lib/moped/cluster.rb b/lib/moped/cluster.rb index b239c03..fa19d96 100644 --- a/lib/moped/cluster.rb +++ b/lib/moped/cluster.rb @@ -174,7 +174,13 @@ def refresh(nodes_to_refresh = seeds) seen = {} # Set up a recursive lambda function for refreshing a node and it's peers. refresh_node = ->(node) do - unless seen[node] + unless node.address.resolved + begin + node.refresh + rescue Errors::ConnectionFailure + end + end + unless seen[node] || !node.address.resolved seen[node] = true # Add the node to the global list of known nodes. seeds.push(node) unless seeds.include?(node) @@ -369,8 +375,10 @@ def initialize_copy(_) # @since 1.0.0 def refresh_peers(node, &block) node.peers.each do |node| - block.call(node) unless seeds.include?(node) - peers.push(node) unless peers.include?(node) + if node.address.resolved + block.call(node) unless seeds.include?(node) + peers.push(node) unless peers.include?(node) + end end end end diff --git a/lib/moped/connection.rb b/lib/moped/connection.rb index 0041867..08aa576 100644 --- a/lib/moped/connection.rb +++ b/lib/moped/connection.rb @@ -46,6 +46,7 @@ def alive? # # @since 1.0.0 def connect + credentials.clear @sock = if !!options[:ssl] Socket::SSL.connect(host, port, timeout) else @@ -74,7 +75,6 @@ def connected? # # @since 1.0.0 def disconnect - credentials.clear @sock.close rescue ensure @@ -217,7 +217,10 @@ def read_data(socket, length) # # @since 1.3.0 def with_connection - connect if @sock.nil? || !@sock.alive? + if @sock.nil? || !@sock.alive? + connect + apply_credentials(@original_credentials || {}) + end yield @sock end end diff --git a/lib/moped/node.rb b/lib/moped/node.rb index 0ede953..4431470 100644 --- a/lib/moped/node.rb +++ b/lib/moped/node.rb @@ -137,7 +137,7 @@ def down? # # @since 1.2.0 def disconnect - connection{ |conn| conn.disconnect } + connection{ |conn| conn.disconnect } if address.resolved true end @@ -175,9 +175,9 @@ def ensure_connected(&block) begin connection do |conn| - stack(:connection) << conn - connect(conn) unless conn.connected? + connect(conn) unless conn.alive? conn.apply_credentials(@credentials) + stack(:connection) << conn yield(conn) end rescue Exception => e diff --git a/spec/moped/node_spec.rb b/spec/moped/node_spec.rb index ee641cb..df034ee 100644 --- a/spec/moped/node_spec.rb +++ b/spec/moped/node_spec.rb @@ -215,6 +215,7 @@ before do node.connection do |conn| conn.stub(:connected?).and_return(true) + conn.stub(:alive?).and_return(false) conn.instance_variable_set(:@sock, nil) end end