Class: Mongo::Srv::Resolver Private

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/mongo/srv/resolver.rb

Overview

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

Encapsulates the necessary behavior for querying SRV records as required by the driver.

Constant Summary collapse

RECORD_PREFIX =

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

Returns RECORD_PREFIX The prefix prepended to each hostname before querying SRV records.

Returns:

  • (String)

    RECORD_PREFIX The prefix prepended to each hostname before querying SRV records.

'_mongodb._tcp.'

Constants included from Loggable

Loggable::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger

Constructor Details

#initialize(**opts) ⇒ Resolver

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.

Creates a new Resolver.

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :timeout (Float)

    The timeout, in seconds, to use for each DNS record resolution.

  • :raise_on_invalid (Boolean)

    Whether or not to raise an exception if either a record with a mismatched domain is found or if no records are found. Defaults to true.

  • :resolv_options (Hash)

    For internal driver use only. Options to pass through to Resolv::DNS constructor for SRV lookups.



49
50
51
52
53
# File 'lib/mongo/srv/resolver.rb', line 49

def initialize(**opts)
  @options = opts.freeze
  @resolver = Resolv::DNS.new(@options[:resolv_options])
  @resolver.timeouts = timeout
end

Instance Attribute Details

#optionsHash (readonly)

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 Resolver options.

Returns:

  • (Hash)

    Resolver options.



56
57
58
# File 'lib/mongo/srv/resolver.rb', line 56

def options
  @options
end

Instance Method Details

#get_records(hostname, srv_service_name = nil, srv_max_hosts = nil) ⇒ Mongo::Srv::Result

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.

Obtains all of the SRV records for a given hostname. If a srv_max_hosts is specified and it is greater than 0, return maximum srv_max_hosts records.

In the event that a record with a mismatched domain is found or no records are found, if the :raise_on_invalid option is true, an exception will be raised, otherwise a warning will be logged.

Parameters:

  • hostname (String)

    The hostname whose records should be obtained.

  • srv_service_name (String | nil) (defaults to: nil)

    The SRV service name for the DNS query. If nil, ‘mongodb’ is used.

  • srv_max_hosts (Integer | nil) (defaults to: nil)

    The maximum number of records to return. If this value is nil, return all of the records.

Returns:

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mongo/srv/resolver.rb', line 82

def get_records(hostname, srv_service_name = nil, srv_max_hosts = nil)
  query_name = record_prefix(srv_service_name) + hostname
  resources = @resolver.getresources(query_name, Resolv::DNS::Resource::IN::SRV)

  # Collect all of the records into a Result object, raising an error
  # or logging a warning if a record with a mismatched domain is found.
  # Note that in the case a warning is raised, the record is _not_
  # added to the Result object.
  result = Srv::Result.new(hostname)
  resources.each do |record|
    result.add_record(record)
  rescue Error::MismatchedDomain => e
    raise if raise_on_invalid?

    log_warn(e.message)
  end

  # If no records are found, either raise an error or log a warning
  # based on the Resolver's :raise_on_invalid option.
  if result.empty?
    raise Error::NoSRVRecords.new(URI::SRVProtocol::NO_SRV_RECORDS % hostname) if raise_on_invalid?

    log_warn(URI::SRVProtocol::NO_SRV_RECORDS % hostname)

  end

  # if srv_max_hosts is in [1, #addresses)
  if (1...result.address_strs.length).include? srv_max_hosts
    sampled_records = resources.sample(srv_max_hosts)
    result = Srv::Result.new(hostname)
    sampled_records.each { |record| result.add_record(record) }
  end
  result
end

#get_txt_options_string(hostname) ⇒ nil | String

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.

Obtains the TXT records of a host.

Parameters:

  • hostname (String)

    The host whose TXT records should be obtained.

Returns:

  • (nil | String)

    URI options string from TXT record associated with the hostname, or nil if there is no such record.

Raises:



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mongo/srv/resolver.rb', line 125

def get_txt_options_string(hostname)
  records = @resolver.getresources(hostname, Resolv::DNS::Resource::IN::TXT)
  return nil if records.empty?

  if records.length > 1
    msg = "Only one TXT record is allowed: querying hostname #{hostname} returned #{records.length} records"

    raise Error::InvalidTXTRecord, msg
  end

  records[0].strings.join
end

#record_prefix(srv_service_name = nil) ⇒ String

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.

Generates the record prefix with a custom SRV service name if it is provided.

Parameters:

  • srv_service_name (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (srv_service_name):

  • The (String | nil)

    SRV service name to use in the record prefix.

Returns:

  • (String)

    The generated record prefix.



36
37
38
# File 'lib/mongo/srv/resolver.rb', line 36

def record_prefix(srv_service_name = nil)
  srv_service_name ? "_#{srv_service_name}._tcp." : RECORD_PREFIX
end

#timeoutObject

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.



58
59
60
# File 'lib/mongo/srv/resolver.rb', line 58

def timeout
  options[:timeout] || Monitor::DEFAULT_TIMEOUT
end