I've got an app hosted on Windows Azure with the MongoLab add-on and I have an issue where the app works perfectly well after I've deployed it, however after a period of a couple minutes inactivity I get a database error:
No replica set primary available for query with ReadPreference PRIMARY
This error does not go away until I restart the website where the same pattern returns - works perfectly until a couple idle minutes when the error returns
My basic app code:
mongoConnect(process.env.CUSTOMCONNSTR_MONGOLAB_URI, {
server: {
auto_reconnect: true,
socketOptions: {
socketTimeoutMS: 120000,
keepAlive: 1
}
},
replSet: {
socketOptions: {
socketTimeoutMS: 120000,
keepAlive: 1
}
}
}).then(function (db) {
//app configure ...
http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});
});
*mongoConnect is a simple "promisified" function of mongoclient.connect
I understand this is best practice, I'm establishing one connection to the db and then reusing that db object for all requests. From reading the likes of https://support.mongolab.com/entries/23009358-Handling-dropped-connections-on-Windows-Azure I've set my sockettimeouts to 2 minutes to prevent any firewalls prematurely killing sockets and auto reconnect to true so they reconnect, however neither setting solves the problem.