node.js - nodejs socket.io doing nothing but connections 100% cpu -
i'm new nodejs , socket.io, when run it reaches cpu 100% (the cpu jumping between 14-103%..) when have 800 users in site. @ first problem saw every few minutes of users disconnected , reconnect after few seconds, commented out of code ended on connect , on disconnect.. still after 5 minutes cpu reaches jumping cpu 14-100%.. don't know check since there's laterally nothing basic stuff.. node version - 0.10.25 socket.io version - 1.0 code snippet -
var redis = require('redis').createclient(), server = require('http').createserver(), request = require('request'), io = require('socket.io').listen(server), sockets = {}; // , memcache = require('memcache') // , cookie = require('cookie') // run http server on port server.listen(3000); console.log('started node sockets.'); // allow authenticated connections - todo: check if doing anything.. :x io.set('authorization', function(handshake, callback) { return callback(null, true); }); io.sockets.on('connection', function(socket) { var id = getuserid(socket); if( id === false) return; // store user's socket sockets[id] = socket; console.log('user ' + id + ' connected'); request({url:"http://www.-----.co/nodeconnect", qs:{id: id}},function(){}) socket.on('disconnect', function() { id = getuserid(socket); if( id === false ) return; // store user's socket delete sockets[id]; console.log('user ' + id + ' disconnect'); request({url:"http://www.----.co/nodedisconnect", qs:{id: id}},function(){}); }); function getuserid(socket) { if( typeof socket.handshake.headers.cookie == 'undefined' ) return false; // hotfix - check var cookie = socket.handshake.headers.cookie; var startat = cookie.indexof('; uid=') + 6; var tempuid = cookie.substr(startat); return tempuid.substr(0, tempuid.indexof(';') ); // return id. }
the server strong server 32cpu, 120gb ram. running nginx, php, nodejs, 1000 ppl.. , should check running without eating of cpu? , btw, how cpu node application should use?
the problem request in connect , disconnect, slow, , since of mass traffic cpu got occupied.
Comments
Post a Comment