New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoreconnect mongo create a lot of connections #6670

Closed
menme95 opened this Issue Jul 5, 2018 · 7 comments

Comments

Projects
None yet
2 participants
@menme95
Copy link

menme95 commented Jul 5, 2018

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
@vkarpov15

This comment has been minimized.

Copy link
Collaborator

vkarpov15 commented Jul 7, 2018

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

@menme95

This comment has been minimized.

Copy link

menme95 commented Jul 9, 2018

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

@menme95

This comment has been minimized.

Copy link

menme95 commented Jul 9, 2018


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();
@vkarpov15

This comment has been minimized.

Copy link
Collaborator

vkarpov15 commented Jul 22, 2018

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

@menme95

This comment has been minimized.

Copy link

menme95 commented Jul 30, 2018

Do you have new information?

@vkarpov15

This comment has been minimized.

Copy link
Collaborator

vkarpov15 commented Aug 4, 2018

@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 vkarpov15 modified the milestones: 5.2.7, 5.2.8 Aug 5, 2018

@vkarpov15 vkarpov15 modified the milestones: 5.2.8, 5.2.9 Aug 12, 2018

vkarpov15 added a commit that referenced this issue Aug 16, 2018

Merge pull request #6869 from simllll/patch-4
Upgrade driver: mongodb to 3.1.3 and mongodb-core to 3.1.2, fixes #6692 & #6670
@vkarpov15

This comment has been minimized.

Copy link
Collaborator

vkarpov15 commented Aug 17, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment