doc_id
doc_id

Reputation: 1443

Heroku nodejs/mongoose and mLab app crash with timeout

Below is the output of heroku logs. I tried setting connection parameters

    socketTimeoutMS: 0,
    connectionTimeout: 0

but still occures

[web.1]: events.js:160
[web.1]:       throw er; // Unhandled 'error' event
[web.1]:       ^
[web.1]: 
[web.1]: Error: connection timeout
/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:169:17)
........
[web.1]: npm ERR! Linux 3.13.0-105-generic
/.heroku/node/bin/npm" "start"
[web.1]: npm ERR! node v6.9.5
[web.1]: npm ERR! npm  v3.10.10
[web.1]: npm ERR! code ELIFECYCLE

Upvotes: 0

Views: 727

Answers (1)

doc_id
doc_id

Reputation: 1443

I contacted mLab for that. That's the response of their support personnel:

We often suggest a 30sec connection timeout when connecting from Heroku. You can see our suggested Mongoose config here: https://gist.github.com/mongolab-org/9959376

Below is the connection configurations in that github gist:

// mongoose 4.3.x
var mongoose = require('mongoose');

/* 
 * Mongoose by default sets the auto_reconnect option to true.
 * We recommend setting socket options at both the server and replica set level.
 * We recommend a 30 second connection timeout because it allows for 
 * plenty of time in most operating environments.
 */
var options = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } }, 
                replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } };       

var mongodbUri = 'mongodb://user:pass@host:port/db';

mongoose.connect(mongodbUri, options);
var conn = mongoose.connection;             

conn.on('error', console.error.bind(console, 'connection error:'));  

conn.once('open', function() {
  // Wait for the database connection to establish, then start the app.                         
});

Upvotes: 2

Related Questions