Module: Mongo::ServerSelector::Selectable
- Included in:
- Nearest, Primary, PrimaryPreferred, Secondary, SecondaryPreferred
- Defined in:
- lib/mongo/server_selector/selectable.rb
Overview
Provides common behavior for filtering a list of servers by server mode or tag set.
Instance Attribute Summary collapse
-
#max_staleness ⇒ Integer
readonly
Max_staleness The maximum replication lag, in seconds, that a secondary can suffer and still be eligible for a read.
-
#options ⇒ Hash
readonly
Options The options.
-
#tag_sets ⇒ Array
readonly
Tag_sets The tag sets used to select servers.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Check equality of two server selector.
-
#candidates(cluster) ⇒ Array<Server>
Get the potential candidates to select from the cluster.
-
#initialize(options = {}) ⇒ Object
Initialize the server selector.
-
#inspect ⇒ String
Inspect the server selector.
-
#local_threshold ⇒ Float
deprecated
Deprecated.
This setting is now taken from the cluster options when a server is selected. Will be removed in 3.0.
-
#select_server(cluster, ping = true) ⇒ Mongo::Server
Select a server from eligible candidates.
-
#server_selection_timeout ⇒ Float
deprecated
Deprecated.
This setting is now taken from the cluster options when a server is selected. Will be removed in 3.0.
Instance Attribute Details
#max_staleness ⇒ Integer (readonly)
Returns max_staleness The maximum replication lag, in seconds, that a secondary can suffer and still be eligible for a read.
33 34 35 |
# File 'lib/mongo/server_selector/selectable.rb', line 33 def max_staleness @max_staleness end |
#options ⇒ Hash (readonly)
Returns options The options.
24 25 26 |
# File 'lib/mongo/server_selector/selectable.rb', line 24 def @options end |
#tag_sets ⇒ Array (readonly)
Returns tag_sets The tag sets used to select servers.
27 28 29 |
# File 'lib/mongo/server_selector/selectable.rb', line 27 def tag_sets @tag_sets end |
Instance Method Details
#==(other) ⇒ true, false
Check equality of two server selector.
45 46 47 48 49 |
# File 'lib/mongo/server_selector/selectable.rb', line 45 def ==(other) name == other.name && tag_sets == other.tag_sets && max_staleness == other.max_staleness end |
#candidates(cluster) ⇒ Array<Server>
Get the potential candidates to select from the cluster.
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/mongo/server_selector/selectable.rb', line 159 def candidates(cluster) if cluster.single? cluster.servers.each { |server| validate_max_staleness_support!(server) } elsif cluster.sharded? near_servers(cluster.servers).each { |server| validate_max_staleness_support!(server) } else validate_max_staleness_value!(cluster) unless cluster.unknown? select(cluster.servers) end end |
#initialize(options = {}) ⇒ Object
Initialize the server selector.
68 69 70 71 72 73 |
# File 'lib/mongo/server_selector/selectable.rb', line 68 def initialize( = {}) @options = ( || {}).freeze @tag_sets = ([:tag_sets] || []).freeze @max_staleness = [:max_staleness] unless [:max_staleness] == -1 validate! end |
#inspect ⇒ String
Inspect the server selector.
83 84 85 |
# File 'lib/mongo/server_selector/selectable.rb', line 83 def inspect "#<#{self.class.name}:0x#{object_id} tag_sets=#{tag_sets.inspect} max_staleness=#{max_staleness.inspect}>" end |
#local_threshold ⇒ Float
This setting is now taken from the cluster options when a server is selected. Will be removed in 3.0.
Get the local threshold boundary for nearest selection in seconds.
145 146 147 |
# File 'lib/mongo/server_selector/selectable.rb', line 145 def local_threshold @local_threshold ||= ([:local_threshold] || ServerSelector::LOCAL_THRESHOLD) end |
#select_server(cluster, ping = true) ⇒ Mongo::Server
Select a server from eligible candidates.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/mongo/server_selector/selectable.rb', line 97 def select_server(cluster, ping = true) @local_threshold = cluster.[:local_threshold] || LOCAL_THRESHOLD @server_selection_timeout = cluster.[:server_selection_timeout] || SERVER_SELECTION_TIMEOUT deadline = Time.now + server_selection_timeout while (deadline - Time.now) > 0 servers = candidates(cluster) if servers && !servers.compact.empty? server = servers.first # There is no point pinging a standalone as the subsequent scan is # not going to change anything about the cluster. if ping && !cluster.single? return server if server.connectable? else return server end end cluster.scan! end raise Error::NoServerAvailable.new(self) end |
#server_selection_timeout ⇒ Float
This setting is now taken from the cluster options when a server is selected. Will be removed in 3.0.
Get the timeout for server selection.
129 130 131 132 |
# File 'lib/mongo/server_selector/selectable.rb', line 129 def server_selection_timeout @server_selection_timeout ||= ([:server_selection_timeout] || ServerSelector::SERVER_SELECTION_TIMEOUT) end |