@@ -49,10 +49,13 @@ func NewClientEntity(ctx context.Context, entityOptions *EntityOptions) (*Client
49
49
return nil , fmt .Errorf ("error parsing URI options: %v" , err )
50
50
}
51
51
}
52
- // UseMultipleMongoses only requires options to be set if we're connected to a sharded cluster and it's set to
53
- // false. If it's unset or true, we make no changes because the cluster URI already includes all mongos nodes.
54
- if mtest .ClusterTopologyKind () == mtest .Sharded && ! entityOptions .UseMultipleMongoses {
55
- clientOpts .SetHosts (mtest .ClusterConnString ().Hosts [:1 ])
52
+ // UseMultipleMongoses is only relevant if we're connected to a sharded cluster. Options changes and validation are
53
+ // only required if the option is explicitly set. If it's unset, we make no changes because the cluster URI already
54
+ // includes all nodes and we don't enforce any limits on the number of nodes.
55
+ if mtest .ClusterTopologyKind () == mtest .Sharded && entityOptions .UseMultipleMongoses != nil {
56
+ if err := evaluateUseMultipleMongoses (clientOpts , * entityOptions .UseMultipleMongoses ); err != nil {
57
+ return nil , err
58
+ }
56
59
}
57
60
if entityOptions .ObserveEvents != nil {
58
61
// Configure a command monitor that listens for the specified event types. We don't take the IgnoredCommands
@@ -183,3 +186,17 @@ func setClientOptionsFromURIOptions(clientOpts *options.ClientOptions, uriOpts b
183
186
}
184
187
return nil
185
188
}
189
+
190
+ func evaluateUseMultipleMongoses (clientOpts * options.ClientOptions , useMultipleMongoses bool ) error {
191
+ hosts := mtest .ClusterConnString ().Hosts
192
+
193
+ if ! useMultipleMongoses {
194
+ clientOpts .SetHosts (hosts [:1 ])
195
+ return nil
196
+ }
197
+
198
+ if len (hosts ) < 2 {
199
+ return fmt .Errorf ("multiple mongoses required but cluster URI %q only contains one host" , mtest .ClusterURI ())
200
+ }
201
+ return nil
202
+ }
0 commit comments