mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Autoreconnect mongo create a lot of connections

Open menme95 opened this issue 8 years ago • 7 comments

Do you want to request a feature or report a bug? Bug What is the current behavior? When my server lost the connection with the database, when it is recovered, I have a lot of connection, the amount of connection is proportional to time.

I've detected this bug with mongoose, but it continuos when I use just mongodb. If the current behaviour is a bug, please provide the steps to reproduce.

  1. Open the database and execute: mongostat
  2. Wait the connection is established.
  3. Stop the connection (not the script)
  4. Restore the connection.
  5. Look the amount of connection created
 
var mongodb = require('mongodb').MongoClient;
var Server = require('mongodb').Server;

var url = 'mongodb://localhost:28410/myDatabase';
var options = {
	useNewUrlParser: true,
	autoReconnect: true,
	poolSize: 3,
	reconnectTries: Number.MAX_VALUE,
	reconnectInterval: 1000
}
var conectDate = new Date();
var count = 0;


// Connect to the db
	mongodb.connect(url, options, function (err, db) {

		
		if (!err) {
			console.log("We are connected");
			db.on('close', function () {
				console.log('Close')
			})
			db.on('reconnect', function () {
				console.log('Reconnect')
			})
		}
		else {
			setTimeout(connectWithRetry, 1000);
			count++;
			console.log(conectDate + ' - Database not connected - ' + count);
		}
	});

**Please mention your node.js, mongoose and MongoDB version.**
I've detected this with node 6.1, 8.9.0 and 10.0.0.
Mongoose versions: 5.1.2
MongoDb driver: 3.0.8
MongoServer: 3.2.13 and 3.4.13
                                    

menme95 avatar Jul 05 '18 10:07 menme95

What is the connectWithRetry() function, can you post that code as well?

vkarpov15 avatar Jul 07 '18 18:07 vkarpov15

Of course, but I've tested this with and without that function , and I've always had the problem.

menme95 avatar Jul 09 '18 07:07 menme95


var mongodb = require('mongodb').MongoClient;
var Server = require('mongodb').Server;

var url = 'mongodb://localhost:28410/edata';
var options = {
	useNewUrlParser: true,
	autoReconnect: true,
	poolSize: 3,
	reconnectTries: Number.MAX_VALUE,
	reconnectInterval: 1000,
	socketTimeoutMS: 5000
}
var conectDate = new Date();
var count = 0;


// Connect to the db
var connectWithRetry = function () {
	mongodb.connect(url, options, function (err, db) {

		
		if (!err) {
			console.log("We are connected");
			db.on('close', function () {
				console.log('Close + ' + count);

			})
			db.on('reconnect', function () {
				console.log('Reconnect')
			})
		}
		else {
			setTimeout(connectWithRetry, 1000);
			count++;
			console.log(conectDate + ' - Database not connected - ' + count);
		}
	});
}

connectWithRetry();

menme95 avatar Jul 09 '18 07:07 menme95

I believe I found the issue in the underlying driver and opened up a PR :point_up: . Will keep this open and track the issue.

vkarpov15 avatar Jul 22 '18 02:07 vkarpov15

Do you have new information?

menme95 avatar Jul 30 '18 13:07 menme95

@menme95 The above PR fixes this issue and will be released when the MongoDB driver ships v3.1.2, Mongoose will ship a release after that to fix this issue.

vkarpov15 avatar Aug 04 '18 15:08 vkarpov15

Can confirm this is fixed in master after we bumped mongodb driver -> 3.1.3

vkarpov15 avatar Aug 17 '18 09:08 vkarpov15