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.

Since:

  • 2.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsHash (readonly)

Returns options The options.

Returns:

  • (Hash)

    options The options.

Since:

  • 2.0.0



24
25
26
# File 'lib/mongo/server_selector/selectable.rb', line 24

def options
  @options
end

#tag_setsArray (readonly)

Returns tag_sets The tag sets used to select servers.

Returns:

  • (Array)

    tag_sets The tag sets used to select servers.

Since:

  • 2.0.0



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.

Examples:

Check server selector equality.

preference == other

Parameters:

  • other (Object)

    The other preference.

Returns:

  • (true, false)

    Whether the objects are equal.

Since:

  • 2.0.0



39
40
41
# File 'lib/mongo/server_selector/selectable.rb', line 39

def ==(other)
  name == other.name && tag_sets == other.tag_sets
end

#initialize(options = {}) ⇒ Object

Initialize the server selector.

Examples:

Initialize the selector.

Mongo::ServerSelector::Secondary.new(:tag_sets => [{'dc' => 'nyc'}])

Initialize the preference with no options.

Mongo::ServerSelector::Secondary.new

Parameters:

  • options (Hash) (defaults to: {})

    The server preference options.

Options Hash (options):

  • :server_selection_timeout (Integer)

    The timeout in seconds for selecting a server.

  • :local_threshold (Integer)

    The local threshold boundary for nearest selection in seconds.

Raises:

Since:

  • 2.0.0



63
64
65
66
67
68
# File 'lib/mongo/server_selector/selectable.rb', line 63

def initialize(options = {})
  @options = (options || {}).freeze
  tag_sets = options[:tag_sets] || []
  validate_tag_sets!(tag_sets)
  @tag_sets = tag_sets.freeze
end

#inspectString

Inspect the server selector.

Examples:

Inspect the server selector.

selector.inspect

Returns:

  • (String)

    The inspection.

Since:

  • 2.2.0



78
79
80
81
# File 'lib/mongo/server_selector/selectable.rb', line 78

def inspect
  "#<#{self.class.name}:0x#{object_id} tag_sets=#{tag_sets.inspect} " +
  "server_selection_timeout=#{server_selection_timeout} local_threshold=#{local_threshold}>"
end

#local_thresholdFloat

Get the local threshold boundary for nearest selection in seconds.

Examples:

Get the local threshold.

selector.local_threshold

Returns:

  • (Float)

    The local threshold.

Since:

  • 2.0.0



133
134
135
# File 'lib/mongo/server_selector/selectable.rb', line 133

def local_threshold
  @local_threshold ||= (options[:local_threshold] || ServerSelector::LOCAL_THRESHOLD)
end

#select_server(cluster, ping = true) ⇒ Mongo::Server

Select a server from eligible candidates.

Examples:

Select a server from the cluster.

selector.select_server(cluster)

Parameters:

  • cluster (Mongo::Cluster)

    The cluster from which to select an eligible server.

Returns:

Raises:

Since:

  • 2.0.0



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/mongo/server_selector/selectable.rb', line 93

def select_server(cluster, ping = true)
  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_timeoutFloat

Get the timeout for server selection.

Examples:

Get the server selection timeout, in seconds.

selector.server_selection_timeout

Returns:

  • (Float)

    The timeout.

Since:

  • 2.0.0



120
121
122
123
# File 'lib/mongo/server_selector/selectable.rb', line 120

def server_selection_timeout
  @server_selection_timeout ||=
    (options[:server_selection_timeout] || ServerSelector::SERVER_SELECTION_TIMEOUT)
end