Class: Mongo::Srv::Resolver Private
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Instance Chain:
self,
::Mongo::Loggable
|
|
| Inherits: | Object |
| Defined in: | lib/mongo/srv/resolver.rb |
Overview
Encapsulates the necessary behavior for querying SRV records as required by the driver.
Constant Summary
-
DNS_ERRORS =
# File 'lib/mongo/srv/resolver.rb', line 38
[ Resolv::ResolvError, Resolv::ResolvTimeout, Timeout::Error, IOError, SystemCallError, SocketError, ].freeze
-
DNS_QUERY_FAILED =
# File 'lib/mongo/srv/resolver.rb', line 34
'The DNS query failed' -
RECORD_PREFIX =
# File 'lib/mongo/srv/resolver.rb', line 28
'_mongodb._tcp.'
::Mongo::Loggable - Included
Class Method Summary
-
.new(**opts) ⇒ Resolver
constructor
Internal use only
Creates a new
Resolver.
Instance Attribute Summary
- #options ⇒ Hash readonly Internal use only
-
#raise_on_invalid? ⇒ Boolean
readonly
private
Internal use only
Checks whether an error should be raised due to either a record with a mismatched domain being found or no records being found.
Instance Method Summary
-
#get_records(hostname, srv_service_name = nil, srv_max_hosts = nil) ⇒ Mongo::Srv::Result
Internal use only
Obtains all of the SRV records for a given hostname.
-
#get_txt_options_string(hostname) ⇒ nil | String
Internal use only
Obtains the TXT records of a host.
-
#record_prefix(srv_service_name = nil) ⇒ String
Internal use only
Generates the record prefix with a custom SRV service name if it is provided.
- #timeout Internal use only
-
#get_resources(query_name, resource_class) ⇒ Array<Resolv::DNS::Resource>
private
Internal use only
Performs a DNS query, replacing any resolver error with a fixed message.
::Mongo::Loggable - Included
| #log_debug | Convenience method to log debug messages with the standard prefix. |
| #log_error | Convenience method to log error messages with the standard prefix. |
| #log_fatal | Convenience method to log fatal messages with the standard prefix. |
| #log_info | Convenience method to log info messages with the standard prefix. |
| #log_warn | Convenience method to log warn messages with the standard prefix. |
| #logger | Get the logger instance. |
| #_mongo_log_prefix, #format_message | |
Constructor Details
.new(**opts) ⇒ Resolver
Creates a new Resolver.
# File 'lib/mongo/srv/resolver.rb', line 66
def initialize(**opts) @options = opts.freeze @resolver = Resolv::DNS.new(@options[:]) @resolver.timeouts = timeout end
Instance Attribute Details
#options ⇒ Hash (readonly)
# File 'lib/mongo/srv/resolver.rb', line 73
attr_reader :
#raise_on_invalid? ⇒ Boolean (readonly, private)
Checks whether an error should be raised due to either a record with a mismatched domain being found or no records being found.
# File 'lib/mongo/srv/resolver.rb', line 176
def raise_on_invalid? @options.fetch(:raise_on_invalid, true) end
Instance Method Details
#get_records(hostname, srv_service_name = nil, srv_max_hosts = nil) ⇒ Mongo::Srv::Result
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.
# File 'lib/mongo/srv/resolver.rb', line 99
def get_records(hostname, srv_service_name = nil, srv_max_hosts = nil) query_name = record_prefix(srv_service_name) + hostname resources = get_resources(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.) 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) if raise_on_invalid? log_warn(URI::SRVProtocol::NO_SRV_RECORDS) 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_resources(query_name, resource_class) ⇒ Array<Resolv::DNS::Resource> (private)
Performs a DNS query, replacing any resolver error with a fixed
message. Resolver error messages quote the query name, and the cause
chain would quote it again, so both are dropped. The exception class
is preserved so callers can still distinguish timeouts from other
failures.
# File 'lib/mongo/srv/resolver.rb', line 166
def get_resources(query_name, resource_class) @resolver.getresources(query_name, resource_class) rescue *DNS_ERRORS => e raise e.class.new("#{DNS_QUERY_FAILED} (#{e.class})"), cause: nil end
#get_txt_options_string(hostname) ⇒ nil | String
Obtains the TXT records of a host.
# File 'lib/mongo/srv/resolver.rb', line 141
def (hostname) records = get_resources(hostname, Resolv::DNS::Resource::IN::TXT) return nil if records.empty? if records.length > 1 msg = "Only one TXT record is allowed (found #{records.length})" raise Error::InvalidTXTRecord, msg end records[0].strings.join end
#record_prefix(srv_service_name = nil) ⇒ String
Generates the record prefix with a custom SRV service name if it is provided.
# File 'lib/mongo/srv/resolver.rb', line 53
def record_prefix(srv_service_name = nil) srv_service_name ? "_#{srv_service_name}._tcp." : RECORD_PREFIX end
#timeout
[ GitHub ]# File 'lib/mongo/srv/resolver.rb', line 75
def timeout [:timeout] || Monitor::DEFAULT_TIMEOUT end