Skip to content

Commit

Permalink
Fix connectionCount
Browse files Browse the repository at this point in the history
connectionCount is unreliable. If you call res.close() the disconnect event can be fired multiple times. Because connectionCount was being decremented in the disconnect handler (called multiple times), you end up with a situation where the number of connections doesn't match the connectionCount.
  • Loading branch information
willyboy committed Dec 10, 2016
1 parent aa40300 commit 69ff5ef
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/sse-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ SseChannel.prototype.addClient = function(req, res, callback) {

// Add the connection to our pool
this.connections.push(res);
this.connectionCount++;
//It would be preferable to not expose this.connectionCount at all and force users to use getConnectionCount
//However, this PR should be backwards compatible. Maybe add a deprecation message?
this.connectionCount = this.connections.length;

// When the client disconnects, remove the client
var removeClient = this.removeClient.bind(this, res);
Expand Down Expand Up @@ -144,7 +146,7 @@ SseChannel.prototype.addClient = function(req, res, callback) {
*/
SseChannel.prototype.removeClient = function(res) {
pull(this.connections, res);
this.connectionCount--;
this.connectionCount = this.connections.length;

this.emit('disconnect', this, res);
};
Expand All @@ -155,7 +157,7 @@ SseChannel.prototype.removeClient = function(res) {
* @return {Number} Number of active connections
*/
SseChannel.prototype.getConnectionCount = function() {
return this.connectionCount;
return this.connections.length;
};

/**
Expand Down

0 comments on commit 69ff5ef

Please sign in to comment.