Skip to content

Commit 47c4bea

Browse files
Adding needed changes to adhere to spec + fixing srv_max_hosts bug
1 parent 1a9c0f8 commit 47c4bea

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

lib/mongo/srv/monitor.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ def do_work
7272
def scan!
7373
begin
7474
last_result = Timeout.timeout(timeout) do
75-
@resolver.get_records(@srv_uri.query_hostname)
75+
@resolver.get_records(
76+
@srv_uri.query_hostname,
77+
@srv_uri.uri_options[:srv_service_name] || options[:srv_service_name],
78+
@srv_uri.uri_options[:srv_max_hosts] || @options[:srv_max_hosts]
79+
)
7680
end
7781
rescue Resolv::ResolvTimeout => e
7882
log_warn("SRV monitor: timed out trying to resolve hostname #{@srv_uri.query_hostname}: #{e.class}: #{e}")

lib/mongo/srv/resolver.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ def timeout
6161
options[:timeout] || Monitor::DEFAULT_TIMEOUT
6262
end
6363

64+
# Fisher-Yates shuffling algorithm helper
65+
# @param [ Array ] array The array to shuffle
66+
# @return [ Array ] The shuffled array
67+
# @api private
68+
def shuffle(array)
69+
a = array.dup
70+
n = a.length
71+
(n - 1).downto(1) do |i|
72+
j = rand(i + 1)
73+
a[i], a[j] = a[j], a[i]
74+
end
75+
a
76+
end
77+
6478
# Obtains all of the SRV records for a given hostname. If a srv_max_hosts
6579
# is specified and it is greater than 0, return maximum srv_max_hosts records.
6680
#
@@ -114,7 +128,7 @@ def get_records(hostname, srv_service_name=nil, srv_max_hosts=nil)
114128

115129
# if srv_max_hosts is in [1, #addresses)
116130
if (1...result.address_strs.length).include? srv_max_hosts
117-
sampled_records = resources.shuffle.first(srv_max_hosts)
131+
sampled_records = shuffle(resources).first(srv_max_hosts)
118132
result = Srv::Result.new(hostname)
119133
sampled_records.each { |record| result.add_record(record) }
120134
end

lib/mongo/uri/srv_protocol.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,13 @@ def raise_invalid_error!(details)
122122
end
123123

124124
# Gets the SRV resolver.
125+
# If domain verification fails or no SRV records are found,
126+
# an error must not be raised per the spec; instead, a warning is logged.
125127
#
126128
# @return [ Mongo::Srv::Resolver ]
127129
def resolver
128130
@resolver ||= Srv::Resolver.new(
129-
raise_on_invalid: true,
131+
raise_on_invalid: false,
130132
resolv_options: options[:resolv_options],
131133
timeout: options[:connect_timeout],
132134
)
@@ -149,7 +151,11 @@ def parse!(remaining)
149151

150152
log_debug "attempting to resolve #{hostname}"
151153

152-
@srv_result = resolver.get_records(hostname, uri_options[:srv_service_name], uri_options[:srv_max_hosts])
154+
@srv_result = resolver.get_records(
155+
hostname,
156+
uri_options[:srv_service_name] || options[:srv_service_name],
157+
uri_options[:srv_max_hosts] || options[:srv_max_hosts]
158+
)
153159
if srv_result.empty?
154160
raise Error::NoSRVRecords.new(NO_SRV_RECORDS % hostname)
155161
end

0 commit comments

Comments
 (0)