Skip to content

Commit

Permalink
A push shouldn't lock NodeJS - added nextTick()
Browse files Browse the repository at this point in the history
A pollbuffer with very many or heavy listeners can keep NodeJS quite busy on a push - especially if many pushes are made at once.

By putting the notification of all listeners on nextTick() we avoid that multiple pushes always results in multiple notifications. If a notification is already due to happen it will wait for that.

By putting each individual notification on nextTick() we ensure that very many listeners or a few heavy ones wont lock NodeJS for too long.
  • Loading branch information
voxpelli authored and robrighter committed Aug 3, 2010
1 parent b1050bc commit 6698dd4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/longpollingbuffer.js 100644 → 100755
Expand Up @@ -6,6 +6,7 @@ var LongPollingBuffer = function (buffersize) {


var data = new Array(); var data = new Array();
var listeners = {}; var listeners = {};
var dirty = false;
this.size = buffersize; this.size = buffersize;
this.offset = 0; this.offset = 0;
this.uid = 0; this.uid = 0;
Expand All @@ -19,8 +20,9 @@ var LongPollingBuffer = function (buffersize) {
} }


var notifyAll = function (){ var notifyAll = function (){
dirty = false;
for(var key in listeners){ for(var key in listeners){
listeners[key](); process.nextTick(listeners[key]);
} }
} }


Expand All @@ -38,7 +40,10 @@ var LongPollingBuffer = function (buffersize) {
while(data.length > this.size){ while(data.length > this.size){
data.pop([]); data.pop([]);
} }
notifyAll(); if (!dirty) {
process.nextTick(notifyAll);
dirty = true;
}
} }


this.addListenerForUpdateSince = function(since, callback) { this.addListenerForUpdateSince = function(since, callback) {
Expand Down

0 comments on commit 6698dd4

Please sign in to comment.