3
\$\begingroup\$

I Using Nodejs v9.10.1 for handle download operation from another application. When using PM2 in fork mode to make alive my code. After 50 minutes, this will show high memory usage.

Server Config is CentOS v6.9, 6 Core cpu, 8GB memory.

Any changes needed on this code or any performance?

And here is my nodejs code (downloadengine.js):

var http = require('http'),
 url = require('url'),
 fs = require('fs');
var mime = require('mime');
var mysql = require('mysql');
var dateFormat = require('dateformat');
var log_file_err = fs.createWriteStream(__dirname + '/NodeError.log', { flags: 'a' });
var MySQLConnection = mysql.createConnection({
 host: "localhost",
 user: "---",
 password: "---",
 database: "---"
});
MySQLConnection.connect(function (err) {
 if (err) throw err;
});
var SelectSQL = function (SQLCommand, callback) {
 MySQLConnection.query(SQLCommand, function (err, data, fields) {
 if (err)
 console.log(err);
 else {
 callback(data);
 }
 });
};
var UpdateSQL = function (SQLCommand, callback) {
 MySQLConnection.query(SQLCommand, function (err, data, fields) {
 if (err)
 console.log(err);
 else {
 callback(data);
 }
 });
};
http.createServer(function (req, res) {
 var Nowdate = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
 var query = url.parse(req.url, true).query;
 process.on('uncaughtException', function (err) {
 log_file_err.write('Caught exception: ' + err.stack + '-> ' + Nowdate + '\n');
 console.log(err.stack);
 });
 var UniqID = query["id"];
 if (typeof UniqID === 'undefined') {
 res.end("Hello!");
 }
 var useragent = req.headers['user-agent'];
 var userip = req.headers['x-forwarded-for'] ||
 req.connection.remoteAddress ||
 req.socket.remoteAddress ||
 (req.connection.socket ? req.connection.socket.remoteAddress : null);
 if (userip.substr(0, 7) == "::ffff:") {
 userip = userip.substr(7)
 }
 function RunUpdateSQL(MySQLQuery) {
 UpdateSQL(MySQLQuery, function (data) {
 });
 }
 function RunSelectSQL() {
 SelectSQL("SELECT * FROM DownloadKey WHERE UniqueidPrimary='" + UniqID + "' order by id desc LIMIT 1", function (data) {
 var OutputInfo = '';
 var FileLoaded = false;
 if (Object.keys(data).length < 1) {
 OutputInfo = "Error Detected";
 res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
 res.end(OutputInfo);
 }
 else {
 var isLarger = new Date(Nowdate) > new Date(data[0].Timestamp);
 if (isLarger) {
 OutputInfo = "Link Expired";
 res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
 res.end(OutputInfo);
 }
 else {
 var newFileName = data[0].FilePath + data[0].FileName;
 if (fs.existsSync(newFileName)) {
 RunUpdateSQL("UPDATE DownloadKey SET Downloads=1 WHERE UniqueidPrimary='" + UniqID + "'");
 res.setHeader('Content-disposition', 'attachment; filename*=UTF-8\'\'' + encodeURIComponent(data[0].FileName));
 res.setHeader('Content-Description', 'File Transfer');
 res.setHeader('Content-Type','application/octet-stream');
 res.setHeader('Content-Transfer-Encoding', 'Binary');
 fs.readFile(newFileName, function (err, content) {
 if (err) {
 OutputInfo = "Link Is not Valid";
 res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
 res.end(OutputInfo);
 } else {
 res.setHeader('Content-Length', content.length);
 res.end(content);
 }
 });
 } else {
 OutputInfo = "Link is Not Valid";
 res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
 res.end(OutputInfo);
 }
 }
 }
 });
 }
 RunUpdateSQL("UPDATE DownloadKey SET UserAgent='" + useragent + "',UserIP='" + userip + "' WHERE UniqueidPrimary='" + UniqID + "'");
 RunSelectSQL();
}).listen(3333);
console.log("NodeJS WebApp running at http://localhost:3333/");
200_success
146k22 gold badges190 silver badges479 bronze badges
asked Apr 9, 2018 at 15:26
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

One problem I see is that RunUpdateSQL and RunSelectSQL are defined inside the server callback. Every time the callback is executed to serve a request, both functions are recreated. The more requests you serve, the more times these functions are created, which slows down your server's ability to respond, which causes requests to queue up. Consider defining these functions outside and passing them the needed info instead, similar to how you're doing SelectSQL and UpdateSQL.

Speaking of SelectSQL and UpdateSQL, they look identical. If all they're doing is just calling MySQLConnection.query, you might be better off calling MySQLConnection.query directly from wherever you're using them.

But all of that is just a wild guess. Your problems can be answered by just profiling your code. Chrome Dev Tools can connect to Node.js by adding the --inspect-brk when executing the script. Then just use the profiling tools provided.

answered Apr 10, 2018 at 2:19
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.