Skip to content

Commit d86ffcf

Browse files
committed
Merge pull request #408 from 3rd-Eden/gc
Garbage collection
2 parents a4ec5aa + ab5beaf commit d86ffcf

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

lib/manager.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ function Manager (server, options) {
100100
self.handleUpgrade(req, socket, head);
101101
});
102102

103+
server.on('close', function () {
104+
clearInterval(self.gc);
105+
});
106+
107+
// run our private gc every 10 seconds
108+
this.gc = setInterval(this.garbageCollection.bind(this), 10000);
109+
103110
for (var i in transports) {
104111
if (transports[i].init) {
105112
transports[i].init(this);
@@ -593,9 +600,10 @@ Manager.prototype.handleClient = function (data, req) {
593600
return;
594601
}
595602

596-
var transport = new transports[data.transport](this, data, req);
603+
var transport = new transports[data.transport](this, data, req)
604+
, handshaken = this.handshaken[data.id];
597605

598-
if (this.handshaken[data.id]) {
606+
if (handshaken) {
599607
if (transport.open) {
600608
if (this.closed[data.id] && this.closed[data.id].length) {
601609
transport.payload(this.closed[data.id]);
@@ -611,6 +619,11 @@ Manager.prototype.handleClient = function (data, req) {
611619
this.onConnect(data.id);
612620
this.store.publish('connect', data.id);
613621

622+
// flag as used
623+
delete handshaken.issued;
624+
this.onHandshake(data.id, handshaken);
625+
this.store.publish('handshake', data.id, handshaken);
626+
614627
// initialize the socket for all namespaces
615628
for (var i in this.namespaces) {
616629
var socket = this.namespaces[i].socket(data.id, true);
@@ -818,7 +831,8 @@ Manager.prototype.handleHandshake = function (data, req, res) {
818831

819832
Manager.prototype.handshakeData = function (data) {
820833
var connection = data.request.connection
821-
, connectionAddress;
834+
, connectionAddress
835+
, date = new Date;
822836

823837
if (connection.remoteAddress) {
824838
connectionAddress = {
@@ -835,11 +849,12 @@ Manager.prototype.handshakeData = function (data) {
835849
return {
836850
headers: data.headers
837851
, address: connectionAddress
838-
, time: (new Date).toString()
852+
, time: date.toString()
839853
, query: data.query
840854
, url: data.request.url
841855
, xdomain: !!data.request.headers.origin
842856
, secure: data.request.connection.secure
857+
, issued: +date
843858
};
844859
};
845860

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

970985
/**
971986
* Declares a socket namespace
987+
*
988+
* @api public
972989
*/
973990

974991
Manager.prototype.of = function (nsp) {
@@ -978,3 +995,26 @@ Manager.prototype.of = function (nsp) {
978995

979996
return this.namespaces[nsp] = new SocketNamespace(this, nsp);
980997
};
998+
999+
/**
1000+
* Perform garbage collection on long living objects and properties that cannot
1001+
* be removed automatically.
1002+
*
1003+
* @api private
1004+
*/
1005+
1006+
Manager.prototype.garbageCollection = function () {
1007+
// clean up unused handshakes
1008+
var ids = Object.keys(this.handshaken)
1009+
, i = ids.length
1010+
, now = Date.now()
1011+
, handshake;
1012+
1013+
while (i--) {
1014+
handshake = this.handshaken[ids[i]];
1015+
1016+
if ('issued' in handshake && (now - handshake.issued) >= 3E4) {
1017+
this.onDisconnect(ids[i]);
1018+
}
1019+
}
1020+
};

0 commit comments

Comments
 (0)