-
Type: New Feature
-
Resolution: Done
-
Priority: Minor - P4
-
Affects Version/s: 1.12.5
-
Component/s: None
-
None
-
Environment:Linux tested with JRuby
-
Fully Compatible
Given how the Mongo::URIParser class works, it is currently not possible to provide a single URI like mongodb://foo/bar, where foo is a DNS round-robin name (i.e. with multiple IP addresses) and use this for load balancing/replica set connections.
(This kind of configuration is convenient, to avoid having to explicitly list all host names in the replica set in the client config. Instead, it is managed by the DNS and can even be made HA with the proper DNS software etc. If the number of applications using a given Mongo replica set is large, not having to reconfigure them all whenever adding/removing nodes from the replica set is seen as preferable.)
This patch fixes this. Since we are using an older, existing application infrastructure I've only done it towards the 1.x branch; we are not using the 2.x branch yet. Hopefully, it should't be too much work to rebase this on top of the current master.
Below is the diff:
diff --git a/lib/mongo/functional/uri_parser.rb b/lib/mongo/functional/uri_parser.rb
index 9a1cd0f..af97d50 100644
— a/lib/mongo/functional/uri_parser.rb
+++ b/lib/mongo/functional/uri_parser.rb
@@ -13,6 +13,7 @@
- limitations under the License.
require 'cgi'
+require 'resolv'
require 'uri'
module Mongo
@@ -322,6 +323,8 @@ module Mongo
"least one node."
end
+ @nodes = resolve_dns_aliases(@nodes)
+
- no user info to parse, exit here
return unless user_info
@@ -405,5 +408,23 @@ module Mongo
raise MongoArgumentError, "connect=direct conflicts with setting a replicaset name"
end
end
+
+ def resolve_dns_aliases(nodes)
+ resolver = Resolv::DNS.new
+ resolved_nodes = nodes.map { |node|
+ host, port = *node
+ addresses = resolver.getaddresses(host)
+
+ if addresses.empty? || addresses.count == 1
+ [node]
+ else
+ addresses.map
+ end
+ }.flatten(1)
+
+ resolved_nodes
+ end
end
end
- related to
-
DRIVERS-201 Resolve names to all dns records (multiple dns A records)
- Closed