socket io集群性能

时间:2014-08-06 07:41:28

标签: node.js socket.io

在尝试扩展我的聊天套接字io应用程序之后,当我试图在同一台机器上建立超过2k的websocket连接时,我遇到了某种墙。 这个数字似乎有点低,考虑到我的本地机器是一个8芯6千兆的公羊,在测试期间只有一半的RAM和cpu被使用。作为websocket和linux的菜鸟我可能还没有完美配置我的服务器到目前为止我将打开文件的最大数量提高到560000,打开套接字的最大数量(maxconn)到60000没有效果。

所以我的问题是: - 使用socket io在本地创建的连接数是否有限制? - 使用这种设置,您可以在一台机器上建立多少连接? - 是否有任何其他Linux设置可以调整以提高性能?

此处'是我用于演示

的代码示例
    cluster = require('cluster')
,http = require('http');

//tell express how to parse post data
/*
app.use(express.json());
app.use(express.urlencoded());*/
//server.listen(9020);


if (cluster.isMaster) {

    process.on('uncaughtException', function (err) {
        console.log(err)
    });

    var numberOfCPUs = require('os').cpus().length;
    for (var i = 0; i < numberOfCPUs; i++) {
        cluster.fork();
    }

    //catch err errors and restart worker
    cluster.on('exit', function(worker, code, signal) {
        console.error('worker ' + worker.process.pid + ' died');
        cluster.fork();
    });
}

if (cluster.isWorker) {

    var authenticatedSockets = new Array();

    var server = http.createServer().listen(9020)
    ,io = require('socket.io').listen(server);

    RedisStore = require('socket.io/lib/stores/redis')
    ,redis      = require('socket.io/node_modules/redis');

    //io.set('log level', 1);
    io.set('store',  new RedisStore({
        redisPub    : redis.createClient(),
        redisSub    : redis.createClient(),
        redisClient : redis.createClient()
    }));


    toolbox = require('./utilities/toolbox.js').toolbox
    ,exception = require('./utilities/exception').exception
    ,async = require('./utilities/async.js').async;


    user = {};
    message = {};

    //tell express how to parse post data
    //app.use(express.json());
    //app.use(express.urlencoded());
    //server.listen(9020);

    process.on('uncaughtException', function (err) {
        console.log(err);
          toolbox.handleException(err);
    });

    io.sockets.on('connection', function (socket) {
        socket.on('auth', function(data){
            if (!toolbox.checkvalue(data, ['id', 'token'])) {
                socket.emit('error', {'type':'auth', 'message':'invalid credentials'});
                socket.disconnect();
                return false;
            } else {
                authenticatedSockets.push(socket);
                console.log(data.id);
                console.log(authenticatedSockets.length + 'on worker : ' + cluster.worker.id);
                console.log('-----------------');

                socket.emit('authenticated');
            }
        });
    });
}

测试结果没有给我任何错误,所以我甚至不知道如何调试这个我只是注意到在2000套接字连接后服务器不再尝试建立新连接但只是继续发出握手以保持现有连接的活着

这是测试输出的一个例子

debug: websocket writing 5:::{"name":"authenticated"}
debug: client authorized
info: handshake authorized 4uuaVHiawHt4-JUlGALu
debug: setting request GET /socket.io/1/websocket/4uuaVHiawHt4-JUlGALu?channel=chat
debug: set heartbeat interval for client 4uuaVHiawHt4-JUlGALu
debug: client authorized for 
debug: websocket writing 1::
4112
258on worker : 5
-----------------
debug: websocket writing 5:::{"name":"authenticated"}
debug: client authorized
info: handshake authorized 3jv46TzFB4cjnqsIF_3n
debug: setting request GET /socket.io/1/websocket/3jv46TzFB4cjnqsIF_3n?channel=chat
debug: set heartbeat interval for client 3jv46TzFB4cjnqsIF_3n
debug: client authorized for 
debug: websocket writing 1::
4113
259on worker : 5 //1999 connecitons after the server just keep existing connectons by emitting handshake
-----------------


    debug: websocket writing 5:::{"name":"authenticated"}
    debug: emitting heartbeat for client dkEc9fB45Gw2cOPoF_4X
    debug: websocket writing 2::
    debug: set heartbeat timeout for client dkEc9fB45Gw2cOPoF_4X
    debug: got heartbeat packet
    debug: cleared heartbeat timeout for client dkEc9fB45Gw2cOPoF_4X
    debug: set heartbeat interval for client dkEc9fB45Gw2cOPoF_4X
    debug: emitting heartbeat for client SSCX4LXmIVHNiYk1F_3_
    debug: websocket writing 2::
    debug: set heartbeat timeout for client SSCX4LXmIVHNiYk1F_3_
    debug: got heartbeat packet
    debug: cleared heartbeat timeout for client SSCX4LXmIVHNiYk1F_3_
    debug: set heartbeat interval for client SSCX4LXmIVHNiYk1F_3_
    debug: emitting heartbeat for client R5u8eM3nJGFaKCLfF_4Y
    debug: websocket writing 2::
    debug: set heartbeat timeout for client R5u8eM3nJGFaKCLfF_4Y
    debug: got heartbeat packet
    debug: cleared heartbeat timeout for client R5u8eM3nJGFaKCLfF_4Y
    debug: set heartbeat interval for client R5u8eM3nJGFaKCLfF_4Y
    debug: emitting heartbeat for client PMTMfInWBQ2B2RenF_4Z
    debug: websocket writing 2::
    debug: set heartbeat timeout for client PMTMfInWBQ2B2RenF_4Z
    debug: got heartbeat packet
    debug: cleared heartbeat timeout for client PMTMfInWBQ2B2RenF_4Z
    debug: set heartbeat interval for client PMTMfInWBQ2B2RenF_4Z
    debug: emitting heartbeat for client c6MWHHTJhzmHFwj2F_4a
    debug: websocket writing 2::

0 个答案:

没有答案