1

I'm using socket.io in order to send message to the user when he join my site and initializing his details using cookie sent by the client. After a while and few refreshes performed my queries stop working.

Here's my code:

io.on('connection', function(socket) {
var user = false;
socket.on('hash', function(hash, gameType) {
    socket.join(gameType);
    query('SELECT * FROM `users` WHERE `hash` = ' + pool.escape(hash), function(err, row) {
        if((err) || (!row.length)) return socket.disconnect();
        user = row[0];
        users[user.steamid] = {
            socket: socket.id,
            balance: parseInt(row[0].balance)
        }
        socket.emit('message', {
            balance: row[0].balance,
            type: 'hello',
            user: row[0].steamid
        });
    }
}

function query(sql, callback) {
console.log(callback);
if (typeof callback === 'undefined') {
    callback = function() {};
}
    pool.getConnection(function(err, connection) {
        if(err) return callback(err);
        logger.info('DB Connection ID: '+connection.threadId);
        connection.query(sql, function(err, rows) {
            if(err) return callback(err);
            connection.release();
            return callback(null, rows);
        });
    });
}

log4js.configure({
appenders: [
    { type: 'console' },
    { type: 'file', filename: 'logs/site.log' }
]
});
var logger = log4js.getLogger();

var pool  = mysql.createPool({
    connectionLimit : 10,
    database: 'test',
    host: 'localhost',
    user: 'root',
    password: 'pw'
});

process.on('uncaughtException', function (err) {
   logger.trace('Strange error');
   logger.debug(err);
});
3
  • I've edited the code check it out! Commented Jun 23, 2017 at 20:32
  • Looks better. You should actually log your error messages instead of just disconnecting the sockets, and it doesn't look like process.on('uncaughtException', ...) will ever be invoked since you don't throw anything anywhere. Commented Jun 23, 2017 at 20:38
  • The problem is that when there are a lot of connections my queries stop executing and the socket is not sending messages to the clients. Commented Jun 23, 2017 at 21:08

1 Answer 1

1

my guess is the reason is you exhaust the connection pool.

if(err) return callback(err); << after some erros here
connection.release(); << not released if there is an error

just release the connection before this line

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.