From 5eff0e5ca77057550bbe4484f8980b83da69ae73 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Fri, 22 Jul 2011 21:37:13 +0200 Subject: [PATCH 1/2] Small clean up, so the the code makes a bit more sense --- lib/manager.js | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/manager.js b/lib/manager.js index 86225386c0..916d2f00af 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -99,6 +99,13 @@ function Manager (server) { self.handleUpgrade(req, socket, head); }); + server.on('close', function () { + clearInterval(self.gc); + }); + + // run our private gc every 10 seconds + this.gc = setInterval(this.garbageCollection.bind(this), 10000); + for (var i in transports) { if (transports[i].init) { transports[i].init(this); @@ -585,9 +592,10 @@ Manager.prototype.handleClient = function (data, req) { return; } - var transport = new transports[data.transport](this, data, req); + var transport = new transports[data.transport](this, data, req) + , handshaken = this.handshaken[data.id]; - if (this.handshaken[data.id]) { + if (handshaken) { if (transport.open) { if (this.closed[data.id] && this.closed[data.id].length) { transport.payload(this.closed[data.id]); @@ -603,6 +611,11 @@ Manager.prototype.handleClient = function (data, req) { this.onConnect(data.id); this.store.publish('connect', data.id); + // flag as used + delete handshaken.issued; + this.onHandshake(data.id, handshaken); + this.store.publish('handshake', data.id, handshaken); + // initialize the socket for all namespaces for (var i in this.namespaces) { var socket = this.namespaces[i].socket(data.id, true); @@ -810,7 +823,8 @@ Manager.prototype.handleHandshake = function (data, req, res) { Manager.prototype.handshakeData = function (data) { var connection = data.request.connection - , connectionAddress; + , connectionAddress + , date = new Date; if (connection.remoteAddress) { connectionAddress = { @@ -827,9 +841,10 @@ Manager.prototype.handshakeData = function (data) { return { headers: data.headers , address: connectionAddress - , time: (new Date).toString() + , time: date.toString() , xdomain: !!data.request.headers.origin , secure: data.request.connection.secure + , issued: +date }; }; @@ -959,6 +974,8 @@ Manager.prototype.checkRequest = function (req) { /** * Declares a socket namespace + * + * @api public */ Manager.prototype.of = function (nsp) { @@ -968,3 +985,26 @@ Manager.prototype.of = function (nsp) { return this.namespaces[nsp] = new SocketNamespace(this, nsp); }; + +/** + * Perform garbage collection on long living objects and properties that cannot + * be removed automatically. + * + * @api private + */ + +Manager.prototype.garbageCollection = function () { + // clean up unused handshakes + var ids = Object.keys(this.handshaken) + , i = ids.length + , now = Date.now() + , handshake; + + while (i--) { + handshake = this.handshaken[ids[i]]; + + if ('issued' in handshake && (now - handshake.issued) >= 3E4) { + this.onDisconnect(ids[i]); + } + } +}; From ab5beaff63ef257ecb1b6721b13b33067b8823ef Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Thu, 4 Aug 2011 20:32:49 +0200 Subject: [PATCH 2/2] Fix --- lib/manager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/manager.js b/lib/manager.js index 64aced34e6..d0d02be680 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -849,7 +849,7 @@ Manager.prototype.handshakeData = function (data) { return { headers: data.headers , address: connectionAddress - , time: (new Date).toString() + , time: date.toString() , query: data.query , url: data.request.url , xdomain: !!data.request.headers.origin