Class: Mongo::Srv::Resolver Private
- Inherits:
-
Object
- Object
- Mongo::Srv::Resolver
- 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.
'_mongodb._tcp.'- DNS_QUERY_FAILED =
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 DNS_QUERY_FAILED Error message used when the DNS resolver raises an error. Resolver error messages quote the query name, which for a malformed connection string may contain part of a credential, so they are replaced with this fixed message.
'The DNS query failed'- DNS_ERRORS =
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 DNS_ERRORS Errors raised by the DNS resolver whose messages may quote the query name.
[ Resolv::ResolvError, Resolv::ResolvTimeout, Timeout::Error, IOError, SystemCallError, SocketError, ].freeze
Constants included from Loggable
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
private
Resolver options.
Instance Method Summary collapse
-
#get_records(hostname, srv_service_name = nil, srv_max_hosts = nil) ⇒ Mongo::Srv::Result
private
Obtains all of the SRV records for a given hostname.
-
#get_txt_options_string(hostname) ⇒ nil | String
private
Obtains the TXT records of a host.
-
#initialize(**opts) ⇒ Resolver
constructor
private
Creates a new Resolver.
-
#record_prefix(srv_service_name = nil) ⇒ String
private
Generates the record prefix with a custom SRV service name if it is provided.
- #timeout ⇒ Object private
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.
66 67 68 69 70 |
# File 'lib/mongo/srv/resolver.rb', line 66 def initialize(**opts) @options = opts.freeze @resolver = Resolv::DNS.new(@options[:resolv_options]) @resolver.timeouts = timeout end |
Instance Attribute Details
#options ⇒ Hash (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.
73 74 75 |
# File 'lib/mongo/srv/resolver.rb', line 73 def @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.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# 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_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.
141 142 143 144 145 146 147 148 149 150 151 152 |
# 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
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.
53 54 55 |
# 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 ⇒ Object
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.
75 76 77 |
# File 'lib/mongo/srv/resolver.rb', line 75 def timeout [:timeout] || Monitor::DEFAULT_TIMEOUT end |