Skip to content

Commit 704b0d3

Browse files
committed
NODE-482 Connections definitely broken when connecting to a replica set in construction
1 parent ddfe99c commit 704b0d3

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

lib/mongo_client.js

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var parse = require('./url_parser')
99

1010
/**
1111
* @fileOverview The **MongoClient** class is a class that allows for making Connections to MongoDB.
12-
*
12+
*
1313
* @example
1414
* var MongoClient = require('mongodb').MongoClient,
1515
* test = require('assert');
@@ -52,7 +52,7 @@ function MongoClient() {
5252
* @param {object} [options.mongos=null] A hash of options to set on the mongos object, see **Mongos** constructor**
5353
* @param {MongoClient~connectCallback} callback The command result callback
5454
* @return {null}
55-
*/
55+
*/
5656
this.connect = MongoClient.connect;
5757
}
5858

@@ -74,21 +74,21 @@ function MongoClient() {
7474
* @param {object} [options.mongos=null] A hash of options to set on the mongos object, see **Mongos** constructor**
7575
* @param {MongoClient~connectCallback} callback The command result callback
7676
* @return {null}
77-
*/
77+
*/
7878
MongoClient.connect = function(url, options, callback) {
7979
var args = Array.prototype.slice.call(arguments, 1);
8080
callback = typeof args[args.length - 1] == 'function' ? args.pop() : null;
8181
options = args.length ? args.shift() : null;
8282
options = options || {};
8383

84-
// Set default empty server options
84+
// Set default empty server options
8585
var serverOptions = options.server || {};
8686
var mongosOptions = options.mongos || {};
8787
var replSetServersOptions = options.replSet || options.replSetServers || {};
8888
var dbOptions = options.db || {};
8989

9090
// If callback is null throw an exception
91-
if(callback == null)
91+
if(callback == null)
9292
throw new Error("no callback function provided");
9393

9494
// Parse the string
@@ -109,10 +109,10 @@ MongoClient.connect = function(url, options, callback) {
109109

110110
// Merge in any replicaset server options
111111
if(replSetServersOptions) {
112-
for(var name in replSetServersOptions) object.rs_options[name] = replSetServersOptions[name];
112+
for(var name in replSetServersOptions) object.rs_options[name] = replSetServersOptions[name];
113113
}
114114

115-
if(replSetServersOptions.ssl
115+
if(replSetServersOptions.ssl
116116
|| replSetServersOptions.sslValidate
117117
|| replSetServersOptions.sslCA
118118
|| replSetServersOptions.sslCert
@@ -128,15 +128,15 @@ MongoClient.connect = function(url, options, callback) {
128128

129129
// Merge in any replicaset server options
130130
if(mongosOptions) {
131-
for(var name in mongosOptions) object.mongos_options[name] = mongosOptions[name];
131+
for(var name in mongosOptions) object.mongos_options[name] = mongosOptions[name];
132132
}
133133

134134
if(typeof object.server_options.poolSize == 'number') {
135135
if(!object.mongos_options.poolSize) object.mongos_options.poolSize = object.server_options.poolSize;
136136
if(!object.rs_options.poolSize) object.rs_options.poolSize = object.server_options.poolSize;
137137
}
138138

139-
if(mongosOptions.ssl
139+
if(mongosOptions.ssl
140140
|| mongosOptions.sslValidate
141141
|| mongosOptions.sslCA
142142
|| mongosOptions.sslCert
@@ -203,17 +203,17 @@ MongoClient.connect = function(url, options, callback) {
203203
// Error
204204
var error = null;
205205
// Set up the Server object
206-
var _server = object.servers[i].domain_socket
206+
var _server = object.servers[i].domain_socket
207207
? new Server(object.servers[i].domain_socket, _server_options)
208208
: new Server(object.servers[i].host, object.servers[i].port, _server_options);
209-
209+
210210
var setName;
211211

212-
var connectFunction = function(__server) {
212+
var connectFunction = function(__server) {
213213
// Attempt connect
214214
new Db(object.dbName, __server, {w:1, native_parser:false}).open(function(err, db) {
215215
// Update number of servers
216-
totalNumberOfServers = totalNumberOfServers - 1;
216+
totalNumberOfServers = totalNumberOfServers - 1;
217217
// If no error do the correct checks
218218
if(!err) {
219219
// Close the connection
@@ -244,27 +244,32 @@ MongoClient.connect = function(url, options, callback) {
244244
callback(new Error("cannot combine a list of replicaset seeds and mongos seeds"));
245245
} catch (err) {
246246
throw err
247-
}
247+
}
248248
})
249249
}
250-
251-
if(totalNumberOfMongodServers == 0
252-
&& totalNumberOfMongosServers == 0
253-
&& object.servers.length == 1) {
250+
251+
if(totalNumberOfMongodServers == 0
252+
&& totalNumberOfMongosServers == 0
253+
&& object.servers.length == 1
254+
&& (!object.rs_options.replicaSet || !object.rs_options.rs_name)) {
254255
var obj = object.servers[0];
255-
serverConfig = obj.domain_socket ?
256+
serverConfig = obj.domain_socket ?
256257
new Server(obj.domain_socket, object.server_options)
257-
: new Server(obj.host, obj.port, object.server_options);
258-
} else if(totalNumberOfMongodServers > 0 || totalNumberOfMongosServers > 0) {
258+
: new Server(obj.host, obj.port, object.server_options);
259+
} else if(totalNumberOfMongodServers > 0
260+
|| totalNumberOfMongosServers > 0
261+
|| object.rs_options.replicaSet || object.rs_options.rs_name) {
259262
var finalServers = object.servers
260263
.filter(function(serverObj) {
261264
return errorServers[serverObj.host + ":" + serverObj.port] == null;
262265
})
263266
.map(function(serverObj) {
264267
return new Server(serverObj.host, serverObj.port, object.server_options);
265268
});
269+
266270
// Clean out any error servers
267271
errorServers = {};
272+
268273
// Set up the final configuration
269274
if(totalNumberOfMongodServers > 0) {
270275
try {
@@ -276,7 +281,7 @@ MongoClient.connect = function(url, options, callback) {
276281
return callback(err, null);
277282
}
278283
} else {
279-
serverConfig = new Mongos(finalServers, object.mongos_options);
284+
serverConfig = new Mongos(finalServers, object.mongos_options);
280285
}
281286
}
282287

@@ -296,12 +301,12 @@ MongoClient.connect = function(url, options, callback) {
296301
// Set up all options etc and connect to the database
297302
_finishConnecting(serverConfig, object, options, callback)
298303
}
299-
});
304+
});
300305
}
301306

302307
// Wrap the context of the call
303-
connectFunction(_server);
304-
}
308+
connectFunction(_server);
309+
}
305310
}
306311

307312
var _setNativeParser = function(db_options) {
@@ -345,7 +350,7 @@ var _finishConnecting = function(serverConfig, object, options, callback) {
345350
// Set up the db options
346351
var db = new Db(object.dbName, serverConfig, object.db_options);
347352
// Open the db
348-
db.open(function(err, db){
353+
db.open(function(err, db){
349354

350355
if(err) {
351356
return process.nextTick(function() {
@@ -379,7 +384,7 @@ var _finishConnecting = function(serverConfig, object, options, callback) {
379384
if(success){
380385
process.nextTick(function() {
381386
try {
382-
callback(null, db);
387+
callback(null, db);
383388
} catch (err) {
384389
if(db) db.close();
385390
throw err
@@ -400,7 +405,7 @@ var _finishConnecting = function(serverConfig, object, options, callback) {
400405
} else {
401406
process.nextTick(function() {
402407
try {
403-
callback(err, db);
408+
callback(err, db);
404409
} catch (err) {
405410
if(db) db.close();
406411
throw err

0 commit comments

Comments
 (0)