Method: Mongo::Srv::Resolver#get_records

Defined in:
lib/mongo/srv/resolver.rb

#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:



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
116
117
118
119
120
121
122
# File 'lib/mongo/srv/resolver.rb', line 84

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|
    begin
      result.add_record(record)
    rescue Error::MismatchedDomain => e
      if raise_on_invalid?
        raise
      else
        log_warn(e.message)
      end
    end
  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?
    if raise_on_invalid?
      raise Error::NoSRVRecords.new(URI::SRVProtocol::NO_SRV_RECORDS % hostname)
    else
      log_warn(URI::SRVProtocol::NO_SRV_RECORDS % hostname)
    end
  end

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