I have been tearing my hair out over this problem pretty much since I began developing my project (12 months ago!), always assuming I would find an answer before I was ready to release... unfortunately, that was not the case!
Basically, I have a fairly straightforward node.js server running on Azure connecting to a MongoLab (now MLab) database using mongoose.
The connection code looks like this:
// Connect to DB
//mongoose.set('debug', true);
mongoose.connect(envConfig.app.db, {
server: {
auto_reconnect: true,
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000,
socketTimeoutMS : 30000,
}
},
replset: {
auto_reconnect: true,
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000,
socketTimeoutMS : 30000,
}
}
}, function (err) {
if (err) winstonLogger.error(err);
});
mongoose.connection.on('connecting', function () {
console.log('Connecting to MongoDB...');
});
mongoose.connection.on('connected', function () {
console.log('MongoDB connected!');
});
mongoose.connection.on('open', function () {
console.log('MongoDB connection opened!');
});
mongoose.connection.on('error', function (err) {
console.error('Error in MongoDb connection: ' + err.stack);
winstonLogger.error(err);
mongoose.disconnect();
});
mongoose.connection.on('disconnected', function () {
winstonLogger.error('MongoDB disconnected!');
mongoose.connect(envConfig.app.db, {
server: {
auto_reconnect: true,
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000,
socketTimeoutMS : 30000,
}
},
replset: {
auto_reconnect: true,
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000,
socketTimeoutMS : 30000,
}
}
});
});
mongoose.connection.on('reconnected', function () {
console.log('MongoDB reconnected!');
});
mongoose.connection.on('close', function () {
console.log('MongoDB closed');
});
All of the extra timeout options, etc you see are part of my attempts to fix this problem (to no avail).
Here is a typical request:
AccessToken.findOne({ token: token })
.maxTime(10000)
.exec(function (err, accessToken) {
// If I even got to here I would be happy
if (err) return done(err);
// If I could consistently get to here, my project would be finished and I would enjoy being alive again
});
So, when I boot up my server everything works great. And it keeps working great... sometimes for days, sometimes hours, sometimes minutes. At some point, however, a request will hit this code and it will just... hang. No timeouts, no errors. Just nada. I have not found any evidence in any logs that show any evidence of what's happening. My express logger finally gives up and I get something like:
POST /api/auth/verify - - ms - -
So at this point, my only option is to restart the server, as I cannot get a db request to complete (or timeout, or show an error) for love nor money.
I have scoured the Internet for a year hoping for a solution, but everything I have tried has resulted in... nothing... the same result. I even tried running my own replica set on Azure instead of using MongoLab. Same problem. My only thinking is that it's an Azure problem with no apparent solution, but I'm really not keen on finding another host. That being said, I don't understand why the Mongoose requests don't timeout or show any error, even when I have set a maxTime...
Here's some info from MongoLab on what I thought might be the problem: http://docs.mlab.com/connecting/#known-issues, but as you can see, I have tried everything they suggest, and much more, with no success.
I am truly at a loss, and the whole situation is simultaneously brain-numbing, heartbreaking and unbelievably infuriating.
If anyone out there has ANY ideas at all, I will love you forever...
Thank you in advance!
-Luke