Module: Mongo::ServerSelector

Extended by:
ServerSelector
Included in:
ServerSelector
Defined in:
lib/mongo/server_selector.rb,
lib/mongo/server_selector/base.rb,
lib/mongo/server_selector/nearest.rb,
lib/mongo/server_selector/primary.rb,
lib/mongo/server_selector/secondary.rb,
lib/mongo/server_selector/primary_preferred.rb,
lib/mongo/server_selector/secondary_preferred.rb

Overview

Functionality for getting an object able to select a server, given a preference.

Since:

  • 2.0.0

Defined Under Namespace

Classes: Base, Nearest, Primary, PrimaryPreferred, Secondary, SecondaryPreferred

Constant Summary collapse

LOCAL_THRESHOLD =

The max latency in seconds between the closest server and other servers considered for selection.

Since:

  • 2.0.0

0.015.freeze
SERVER_SELECTION_TIMEOUT =

How long to block for server selection before throwing an exception.

Since:

  • 2.0.0

30.freeze
SMALLEST_MAX_STALENESS_SECONDS =

The smallest allowed max staleness value, in seconds.

Since:

  • 2.4.0

90
PRIMARY =

Primary read preference.

Since:

  • 2.1.0

Options::Redacted.new(mode: :primary).freeze
PREFERENCES =

Hash lookup for the selector classes based off the symbols

provided in configuration.

Since:

  • 2.0.0

{
  nearest: Nearest,
  primary: Primary,
  primary_preferred: PrimaryPreferred,
  secondary: Secondary,
  secondary_preferred: SecondaryPreferred
}.freeze

Instance Method Summary collapse

Instance Method Details

#get(preference = {}) ⇒ Object

Create a server selector object.

Examples:

Get a server selector object for selecting a secondary with

specific tag sets.
Mongo::ServerSelector.get(:mode => :secondary, :tag_sets => [{'dc' => 'nyc'}])

Parameters:

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

    The server preference.

Since:

  • 2.0.0



75
76
77
78
79
# File 'lib/mongo/server_selector.rb', line 75

def get(preference = {})
  return preference if PREFERENCES.values.include?(preference.class)
  Mongo::Lint.validate_underscore_read_preference(preference)
  PREFERENCES.fetch((preference[:mode] || :primary).to_sym).new(preference)
end

#primaryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the primary server selector.

A call to this method is equivalent to ‘get(mode: :primary)`, except the resulting server selector object is cached and not recreated each time.

Since:

  • 2.0.0



87
88
89
# File 'lib/mongo/server_selector.rb', line 87

def primary
  @primary ||= get(mode: :primary)
end