Skip to content

Commit

Permalink
Re-organize Node methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bernerdschaefer committed Apr 16, 2012
1 parent bd5d1e3 commit ab2dbee
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions lib/moped/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ class ReplicaSetReconfigured < StandardError; end

class Node

attr_reader :host, :port, :timeout
attr_reader :host
attr_reader :port
attr_reader :timeout

def initialize(address)
host, port = address.split(":")
Expand Down Expand Up @@ -92,6 +94,12 @@ def refresh
end
end

attr_reader :down_at

def down?
@down_at
end

# Set a flag on the node for the duration of provided block so that an
# exception is raised if the node is no longer the primary node.
#
Expand All @@ -103,35 +111,6 @@ def ensure_primary
Threaded.end :ensure_primary
end

# Connect to the node.
#
# Returns nothing.
# Raises Moped::ConnectionError if the connection times out.
# Raises Moped::ConnectionError if the server is unavailable.
def connect
connection.connect host, port, timeout
@down_at = nil

refresh
rescue Timeout::Error
raise ConnectionError, "Timed out connection to Mongo on #{host}:#{port}"
rescue Errno::ECONNREFUSED
raise ConnectionError, "Could not connect to Mongo on #{host}:#{port}"
end

attr_reader :down_at

# Mark the node as down.
#
# Returns nothing.
def down!
@down_at = Time.now
end

def down?
@down_at
end

# Yields the block if a connection can be established, retrying when a
# connection error is raised.
#
Expand Down Expand Up @@ -204,6 +183,29 @@ def connected?
connection.connected?
end

# Mark the node as down.
#
# Returns nothing.
def down!
@down_at = Time.now
end

# Connect to the node.
#
# Returns nothing.
# Raises Moped::ConnectionError if the connection times out.
# Raises Moped::ConnectionError if the server is unavailable.
def connect
connection.connect host, port, timeout
@down_at = nil

refresh
rescue Timeout::Error
raise ConnectionError, "Timed out connection to Mongo on #{host}:#{port}"
rescue Errno::ECONNREFUSED
raise ConnectionError, "Could not connect to Mongo on #{host}:#{port}"
end

def process(operation, &callback)
if Threaded.executing? :pipeline
queue.push [operation, callback]
Expand Down

0 comments on commit ab2dbee

Please sign in to comment.