lib/mongo/server_selector/selectable.rb in mongo-2.1.0.beta vs lib/mongo/server_selector/selectable.rb in mongo-2.1.0.rc0

- old
+ new

@@ -18,21 +18,10 @@ # Provides common behavior for filtering a list of servers by server mode or tag set. # # @since 2.0.0 module Selectable - # The max latency in seconds between the closest server and other servers - # considered for selection. - # - # @since 2.0.0 - LOCAL_THRESHOLD = 0.015.freeze - - # How long to block for server selection before throwing an exception. - # - # @since 2.0.0 - SERVER_SELECTION_TIMEOUT = 30.freeze - # @return [ Hash ] options The options. attr_reader :options # @return [ Array ] tag_sets The tag sets used to select servers. attr_reader :tag_sets @@ -51,26 +40,31 @@ name == other.name && tag_sets == other.tag_sets end # Initialize the server selector. # - # @example Initialize the preference with tag sets. - # Mongo::ServerSelector::Secondary.new([{ 'tag' => 'set' }]) + # @example Initialize the selector. + # Mongo::ServerSelector::Secondary.new(:tag_sets => [{'dc' => 'nyc'}]) # # @example Initialize the preference with no options. # Mongo::ServerSelector::Secondary.new # - # @param [ Array ] tag_sets The tag sets used to select servers. + # @param [ Hash ] options The server preference options. # - # @todo: document specific error - # @raise [ Exception ] If tag sets are specified but not allowed. + # @option options [ Integer ] :server_selection_timeout The timeout in seconds + # for selecting a server. # + # @option options [ Integer ] :local_threshold The local threshold boundary for + # nearest selection in seconds. + # + # @raise [ Error::InvalidServerPreference ] If tag sets are specified + # but not allowed. + # # @since 2.0.0 - def initialize(tag_sets = [], options = {}) - if !tag_sets.all? { |set| set.empty? } && !tags_allowed? - raise Error::InvalidServerPreference.new(name) - end + def initialize(options = {}) + tag_sets = options[:tag_sets] || [] + validate_tag_sets!(tag_sets) @tag_sets = tag_sets @options = options end # Select a server from eligible candidates. @@ -107,11 +101,11 @@ # @return [ Float ] The timeout. # # @since 2.0.0 def server_selection_timeout @server_selection_timeout ||= - (options[:server_selection_timeout] || SERVER_SELECTION_TIMEOUT) + (options[:server_selection_timeout] || ServerSelector::SERVER_SELECTION_TIMEOUT) end # Get the local threshold boundary for nearest selection in seconds. # # @example Get the local threshold. @@ -119,11 +113,11 @@ # # @return [ Float ] The local threshold. # # @since 2.0.0 def local_threshold - @local_threshold ||= (options[:local_threshold] || LOCAL_THRESHOLD) + @local_threshold ||= (options[:local_threshold] || ServerSelector::LOCAL_THRESHOLD) end private # Select the primary from a list of provided candidates. @@ -183,9 +177,17 @@ tag_sets.find do |tag_set| matches = candidates.select { |server| server.matches_tag_set?(tag_set) } !matches.empty? end matches || [] + end + + private + + def validate_tag_sets!(tag_sets) + if !tag_sets.all? { |set| set.empty? } && !tags_allowed? + raise Error::InvalidServerPreference.new(name) + end end end end end