class Mongo::Srv::Result

SRV record lookup result.

Contains server addresses that the query resolved to, and minimum TTL of the DNS records.

@api private

Constants

MISMATCHED_DOMAINNAME

@return [ String ] MISMATCHED_DOMAINNAME Error message format string indicating that an SRV

record found does not match the domain of a hostname.

Attributes

address_strs[R]

@return [ Array<String> ] address_strs The host strings of the SRV records

for the query hostname.
min_ttl[RW]

@return [ Integer | nil ] min_ttl The smallest TTL found among the

records (or nil if no records have been added).
query_hostname[R]

@return [ String ] query_hostname The hostname pointing to the DNS records.

Public Class Methods

new(hostname) click to toggle source

Create a new object to keep track of the SRV records of the hostname.

@param [ String ] hostname The hostname pointing to the DNS records.

# File lib/mongo/srv/result.rb, line 47
def initialize(hostname)
  @query_hostname = hostname
  @address_strs = []
  @min_ttl = nil
end

Public Instance Methods

add_record(record) click to toggle source

Adds a new record.

@param [ Resolv::DNS::Resource ] record An SRV record found for the hostname.

# File lib/mongo/srv/result.rb, line 63
def add_record(record)
  record_host = normalize_hostname(record.target.to_s)
  port = record.port
  validate_hostname!(record_host)
  validate_same_origin!(record_host)
  address_str = if record_host.index(':')
    # IPV6 address
    "[#{record_host}]:#{port}"
  else
    "#{record_host}:#{port}"
  end
  @address_strs << address_str

  if @min_ttl.nil?
    @min_ttl = record.ttl
  else
    @min_ttl = [@min_ttl, record.ttl].min
  end

  nil
end
empty?() click to toggle source

Checks whether there are any records.

@return [ Boolean ] Whether or not there are any records.

# File lib/mongo/srv/result.rb, line 56
def empty?
  @address_strs.empty?
end

Private Instance Methods

normalize_hostname(host) click to toggle source

Transforms the provided hostname to simplify its validation later on.

This method is safe to call during both initial DNS seed list discovery and during SRV monitoring, in that it does not convert invalid hostnames into valid ones.

  • Converts the hostname to lower case.

  • Removes one trailing dot, if there is exactly one. If the hostname has multiple trailing dots, it is unchanged.

@param [ String ] host Hostname to transform.

# File lib/mongo/srv/result.rb, line 98
def normalize_hostname(host)
  host = host.downcase
  unless host.end_with?('..')
    host = host.sub(/\.\z/, '')
  end
  host
end
validate_same_origin!(record_host) click to toggle source

Ensures that a record's domain name matches that of the hostname.

A hostname's domain name consists of each of the '.' delineated parts after the first. For example, the hostname 'foo.bar.baz' has the domain name 'bar.baz'.

@param [ String ] record_host The host of the SRV record.

@raise [ Mongo::Error::MismatchedDomain ] If the record's domain name doesn't match that of

the hostname.
# File lib/mongo/srv/result.rb, line 116
def validate_same_origin!(record_host)
  domain_name ||= query_hostname.split('.')[1..-1]
  host_parts = record_host.split('.')

  unless (host_parts.size > domain_name.size) && (domain_name == host_parts[-domain_name.length..-1])
    raise Error::MismatchedDomain.new(MISMATCHED_DOMAINNAME % [record_host, domain_name])
  end
end