简体   繁体   中英

node.js express cluster and high CPU usage

My node.js app uses express, socket.io and talks to mongodb through mongoose. All these are working fine with low cpu usage. When I made the app run with cluster, it works fine, but the CPU usage really goes very high. Here is what i am doing.

var settings = require("./settings"),
    cluster = require('cluster');

cluster('./server')
  .use(cluster.logger('logs'))
  .use(cluster.stats())
  .use(cluster.pidfiles('pids'))
  .use(cluster.cli())
  .use(cluster.repl(8888))
  .listen(7777);

When I check the master.log, I see

[Fri, 21 Oct 2011 02:59:51 GMT] INFO master started
[Fri, 21 Oct 2011 02:59:53 GMT] ERROR worker 0 died
[Fri, 21 Oct 2011 02:59:53 GMT] INFO spawned worker 0
[Fri, 21 Oct 2011 02:59:54 GMT] ERROR worker 0 died
[Fri, 21 Oct 2011 02:59:54 GMT] INFO spawned worker 0
[Fri, 21 Oct 2011 02:59:56 GMT] ERROR worker 0 died
[Fri, 21 Oct 2011 02:59:56 GMT] INFO spawned worker 0
.....

[Fri, 21 Oct 2011 03:11:08 GMT] INFO spawned worker 0
[Fri, 21 Oct 2011 03:11:10 GMT] WARNING shutting down master
[Fri, 21 Oct 2011 03:12:07 GMT] INFO spawned worker 0
[Fri, 21 Oct 2011 03:12:07 GMT] INFO spawned worker 1
[Fri, 21 Oct 2011 03:12:07 GMT] INFO master started
[Fri, 21 Oct 2011 03:12:09 GMT] ERROR worker 1 died
[Fri, 21 Oct 2011 03:12:09 GMT] INFO spawned worker 1
[Fri, 21 Oct 2011 03:12:10 GMT] ERROR worker 1 died
[Fri, 21 Oct 2011 03:12:10 GMT] INFO spawned worker 1

In workers.access.log, I see all console messages, socket.io logs etc...

In workers.error.log, I see the following error messages, looks like something wrong...

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: EADDRINUSE, Address already in use
    at HTTPServer._doListen (net.js:1106:5)
    at net.js:1077:14
    at Object.lookup (dns.js:153:45)
    at HTTPServer.listen (net.js:1071:20)
    at Object.<anonymous> (/cygdrive/c/HTML5/RENT/test/server/server.js:703:5)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at require (module.js:346:19)

server.js:703 - points to app.listen(9999);

EDIT: server.js code

var express = require("express"),
    fs = require("fs"),
    form = require('connect-form'),
    app = module.exports = express.createServer(
        form({ keepExtensions: true })
    ),
    sys = require("sys"),
    RentModel = require("./rent_schema"),
    UserModel   = require("./track_schema"),
    email   = require("./email_connect"),
    SubscriptionModel = require("./subscription_schema"),
    io = require("socket.io"),
    fb = require('facebook-js'),
    Twitter = require('./Twitter_Analysis'),
    Foursquare = require('./Foursquare_Analysis'),
    YQL = require("yql"),
    settings = require("./settings");
//    


 var cluster = require('cluster');
 cluster(app)
  .use(cluster.logger('logs'))
  .use(cluster.stats())
  .use(cluster.pidfiles('pids'))
  .use(cluster.cli())
  .use(cluster.debug())
  .use(cluster.repl(settings.ADMIN_PORT))
  .listen(settings.PORT);



socket = io.listen(app);
.....
.....
//app.listen(settings.PORT);

It looks like you're trying to bind your workers with the same port, that is crashing the workers, but cluster is restarting the workers. So you're in an infinite death cycle.

I'm not sure if you need the app.listen(9999) in your server.js file, which is probably trying to bind port 9999 in all your workers. See the examples in the cluster package for a good example: https://github.com/LearnBoost/cluster/blob/master/examples/express.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM