-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: 2.0.0
-
Component/s: None
-
None
Mongo::Cluster::Mode::Standalone.servers erroneously returns [nil] if the configuration is empty, for example, before cluster.servers.scan! or when the server is down and cluster.servers.scan! is run.
The current code is:
module Mongo class Cluster module Mode class Standalone def self.servers(servers, name = nil) [ servers.detect{ |server| server.standalone? } ] end end end end end
Enumerable#detect returns nil if there is no match, which causes other complicated errors up the call chain, for example in Mongo::ServerPreference::Selectable#primary. It smells like a definite bug since Enumerable#select would be better, although other issues still exist up the chain.
The following is a possible improvement, with a check for now to catch other errors that have occurred, for example with a standalone client connecting to a single member of a replica set.
module Mongo class Cluster module Mode class Standalone def self.servers(servers, name = nil) raise "#{self.name}.#{__method__}: only one server expected, servers: #{servers.inspect}" if servers.size != 1 servers end end end end
An alternative would be to somehow make Mongo::Server::Description#standalone mode aware.