minor: cleanup and organization of Connection classes

This commit is contained in:
Kyle Banker 2010-12-15 12:36:43 -05:00
parent b8bb480c2c
commit 9ea8fe98b7
2 changed files with 67 additions and 27 deletions

View File

@ -35,8 +35,7 @@ module Mongo
STANDARD_HEADER_SIZE = 16 STANDARD_HEADER_SIZE = 16
RESPONSE_HEADER_SIZE = 20 RESPONSE_HEADER_SIZE = 20
attr_reader :logger, :size, :nodes, :auths, :primary, :secondaries, :arbiters, attr_reader :logger, :size, :auths, :primary, :safe, :primary_pool, :host_to_try
:safe, :primary_pool, :read_pool, :secondary_pools, :host_to_try
# Counter for generating unique request ids. # Counter for generating unique request ids.
@@current_request_id = 0 @@current_request_id = 0
@ -316,18 +315,7 @@ module Mongo
self["admin"].command(oh) self["admin"].command(oh)
end end
# Increment and return the next available request id. # Get the build information for the current connection.
#
# return [Integer]
def get_request_id
request_id = ''
@id_lock.synchronize do
request_id = @@current_request_id += 1
end
request_id
end
# Get the build information for the current connection.
# #
# @return [Hash] # @return [Hash]
def server_info def server_info
@ -346,7 +334,7 @@ module Mongo
# #
# @return [Boolean] # @return [Boolean]
def slave_ok? def slave_ok?
@read_secondary || @slave_ok @slave_ok
end end
# Send a message to MongoDB, adding the necessary headers. # Send a message to MongoDB, adding the necessary headers.
@ -446,7 +434,9 @@ module Mongo
set_primary(@host_to_try) set_primary(@host_to_try)
end end
raise ConnectionFailure, "failed to connect to any given host:port" unless connected? if !connected?
raise ConnectionFailure, "Failed to connect to a master node at #{@host_to_try[0]}:#{@host_to_try[1]}"
end
end end
def connecting? def connecting?
@ -467,33 +457,29 @@ module Mongo
end end
# Checkout a socket for reading (i.e., a secondary node). # Checkout a socket for reading (i.e., a secondary node).
# Note: this is overridden in ReplSetConnection.
def checkout_reader def checkout_reader
connect unless connected? connect unless connected?
@primary_pool.checkout
if @read_pool
@read_pool.checkout
else
checkout_writer
end
end end
# Checkout a socket for writing (i.e., a primary node). # Checkout a socket for writing (i.e., a primary node).
# Note: this is overridden in ReplSetConnection.
def checkout_writer def checkout_writer
connect unless connected? connect unless connected?
@primary_pool.checkout @primary_pool.checkout
end end
# Checkin a socket used for reading. # Checkin a socket used for reading.
# Note: this is overridden in ReplSetConnection.
def checkin_reader(socket) def checkin_reader(socket)
if @read_pool if @primary_pool
@read_pool.checkin(socket) @primary_pool.checkin(socket)
else
checkin_writer(socket)
end end
end end
# Checkin a socket used for writing. # Checkin a socket used for writing.
# Note: this is overridden in ReplSetConnection.
def checkin_writer(socket) def checkin_writer(socket)
if @primary_pool if @primary_pool
@primary_pool.checkin(socket) @primary_pool.checkin(socket)
@ -616,6 +602,9 @@ module Mongo
apply_saved_authentication apply_saved_authentication
end end
## Low-level connection methods.
def receive(sock, expected_response) def receive(sock, expected_response)
begin begin
receive_header(sock, expected_response) receive_header(sock, expected_response)
@ -727,6 +716,17 @@ module Mongo
request_id request_id
end end
# Increment and return the next available request id.
#
# return [Integer]
def get_request_id
request_id = ''
@id_lock.synchronize do
request_id = @@current_request_id += 1
end
request_id
end
# Low-level method for sending a message on a socket. # Low-level method for sending a message on a socket.
# Requires a packed message and an available socket, # Requires a packed message and an available socket,
# #

View File

@ -111,6 +111,13 @@ module Mongo
@nodes_to_try = [] @nodes_to_try = []
end end
# Is it okay to connect to a slave?
#
# @return [Boolean]
def slave_ok?
@read_secondary || @slave_ok
end
private private
def check_is_master(node) def check_is_master(node)
@ -212,5 +219,38 @@ module Mongo
@nodes_to_try = new_nodes - @nodes_tried @nodes_to_try = new_nodes - @nodes_tried
end end
# Checkout a socket for reading (i.e., a secondary node).
def checkout_reader
connect unless connected?
if @read_pool
@read_pool.checkout
else
checkout_writer
end
end
# Checkout a socket for writing (i.e., a primary node).
def checkout_writer
connect unless connected?
@primary_pool.checkout
end
# Checkin a socket used for reading.
def checkin_reader(socket)
if @read_pool
@read_pool.checkin(socket)
else
checkin_writer(socket)
end
end
# Checkin a socket used for writing.
def checkin_writer(socket)
if @primary_pool
@primary_pool.checkin(socket)
end
end
end end
end end