Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Garbage collection #408

Merged
merged 4 commits into from Aug 4, 2011
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 44 additions & 4 deletions lib/manager.js
Expand Up @@ -100,6 +100,13 @@ function Manager (server, options) {
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);
Expand Down Expand Up @@ -593,9 +600,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]);
Expand All @@ -611,6 +619,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);
Expand Down Expand Up @@ -818,7 +831,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 = {
Expand All @@ -835,11 +849,12 @@ 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
, secure: data.request.connection.secure
, issued: +date
};
};

Expand Down Expand Up @@ -969,6 +984,8 @@ Manager.prototype.checkRequest = function (req) {

/**
* Declares a socket namespace
*
* @api public
*/

Manager.prototype.of = function (nsp) {
Expand All @@ -978,3 +995,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]);
}
}
};