I have a simple app using Express.js that handles posted data from remote systems.
There is basic authentication (express.basicAuth) and a handful of other modules (generic-pool for DB connection pooling, moment.js for time calculations, and mysql) as well as Forever to run the app as a service. The app is running on SuSe 11.2 in case that matters.
Everything runs perfectly fine for approximately 2 hours - data is posted, processed, then stored. Roughly 1kb~ of data per post, 80 posts per hour. After that the app becomes unresponsive until the app is restarted ($ forever restart)
I'm quite new to Node and don't know where to go next to debug the problem. Nothing is being written to the access or error log files when the app goes unresponsive, and not sure if there is another way to get meaningful output or a stack trace.
Any advice or direction would be appreciated!
Edit: As added in the comments, the data is parsed and written directly to a MySQL table using generic-pool for the connection management.
Specifically:
var poolModule = require('generic-pool');
var pool = poolModule.Pool({
name: 'mysql',
create: function(cb) {
var mysql = require('mysql');
var db = mysql.createConnection({ host: '...', user: '...', 'password: '...' });
cb(null, db);
},
destroy: function(client) { client.end(); },
max: 10
});
Then in the router:
function(req, res) {
pool.acquire(function(err,client)) {
if(err) throw err;
// ...data parsing / query building
client.query('...the query...', function(err, rows, fields) {
pool.release(client);
if(err) { res.send(err); }
else { res.send('success'); }
}
});
}
Edit2 - it looks like a failure event is occurring which results in the db connection not closing. I noticed 2 connections open for 5 and 15 minutes.. given this rate of growth the max connection limit (10) could easily be reached and cause the hanging. I'll know soon enough if this is the culprit! :)
The DB connection wasn't being released when an error was encountered. When the connection limit was reached eventually reached subsequent page requests hung. After improving the data processing and error handling the problem went away.
How about trying to maintain your db connection like below?
setInterval(function(){
// Run query to retain db connection
db.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
});
}, 60 * 1000);