开发者

Long Running Node.js process slows down over days of running

I am running a node.js script on EC2 to monitor and run a node HTTP server as a child process.

Unfortunately this child server slows down slowly, requests that take 0.2 seconds start dragging out, after days the same requests take over 2 seconds.

As part of debugging this, I implemented a 2 hour restart to kill the child server and start another one. This has no effect! The HTTP server child process is restarted, but it is still slow! Only restarting this parent script makes the child faster.

Why is the HTTP server slowing down, even when killed and restarted?

Environment is 0.4.9 Node.js on EC2 Ubuntu server. Parent script is below.

var http = require('http');

var server,
    firstOperated = null;
    lastOperated = null;

function operating(str) { 
    r开发者_开发问答eturn (str.toString().substring(0, 13) != 'SERVER ONLINE') ? log(str) :
        lastOperated = new Date();
}

function log(str) {
    str = str.toString('utf8');

    if (str.length) console.log(str.replace(/\n+$/gim, ''));
}

function createServer() {
    if (server) {
        server.kill('SIGKILL');

        return console.log('KILLED NON RESPONSIVE SERVER');        
    }

    server = require('child_process').spawn('node', [__dirname + '/http.js', 80]);

    firstOperated = new Date();

    server.stdout.on('data', operating);
    server.stderr.on('data', log);

    server.on('exit', function(code) { 
        lastOperated = null;
        server = null;

        console.log("SERVER EXITED: " + code); 
    });
}

createServer();

setInterval(function() { 
    if (new Date() - firstOperated > 1000 * 60 * 60 * 2) return createServer();

    if (new Date() - lastOperated < 5 * 1000) return; // server seems to be operating ok

    createServer();
}, 5 * 1000);


If the EC2 instance is a Micro instance, and you are running at high cpu for more than about 15 seconds usage then you will be throttled (severely). This would explain the symptoms. The solution would be to scale up to a small instance. (It would not have to be the Node process that consumes the cpu cycles).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜