lib/mongo/server_selector/selectable.rb in mongo-2.1.0.rc0 vs lib/mongo/server_selector/selectable.rb in mongo-2.1.0
- old
+ new
@@ -59,14 +59,14 @@
# @raise [ Error::InvalidServerPreference ] If tag sets are specified
# but not allowed.
#
# @since 2.0.0
def initialize(options = {})
+ @options = (options || {}).freeze
tag_sets = options[:tag_sets] || []
validate_tag_sets!(tag_sets)
- @tag_sets = tag_sets
- @options = options
+ @tag_sets = tag_sets.freeze
end
# Select a server from eligible candidates.
#
# @example Select a server from the cluster.
@@ -78,18 +78,15 @@
#
# @since 2.0.0
def select_server(cluster)
deadline = Time.now + server_selection_timeout
while (deadline - Time.now) > 0
- if cluster.single?
- servers = cluster.servers
- elsif cluster.sharded?
- servers = near_servers(cluster.servers)
- else
- servers = select(cluster.servers)
+ servers = candidates(cluster)
+ if servers && !servers.compact.empty?
+ server = servers.first
+ return server if server.connectable?
end
- return servers.first if servers && !servers.compact.empty?
cluster.scan!
end
raise Error::NoServerAvailable.new(self)
end
@@ -117,9 +114,19 @@
def local_threshold
@local_threshold ||= (options[:local_threshold] || ServerSelector::LOCAL_THRESHOLD)
end
private
+
+ def candidates(cluster)
+ if cluster.single?
+ cluster.servers
+ elsif cluster.sharded?
+ near_servers(cluster.servers)
+ else
+ select(cluster.servers)
+ end
+ end
# Select the primary from a list of provided candidates.
#
# @param [ Array ] candidates List of candidate servers to select the
# primary from.