Permalink
Browse files

A push shouldn't lock NodeJS - added nextTick()

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...
1 parent b1050bc commit 6698dd476151682987e74283c47c2e8fccf3d54b @voxpelli voxpelli committed with Aug 3, 2010
Showing with 7 additions and 2 deletions.
  1. +7 −2 lib/longpollingbuffer.js
View
9 lib/longpollingbuffer.js 100644 → 100755
@@ -6,6 +6,7 @@ var LongPollingBuffer = function (buffersize) {
var data = new Array();
var listeners = {};
+ var dirty = false;
this.size = buffersize;
this.offset = 0;
this.uid = 0;
@@ -19,8 +20,9 @@ var LongPollingBuffer = function (buffersize) {
}
var notifyAll = function (){
+ dirty = false;
for(var key in listeners){
- listeners[key]();
+ process.nextTick(listeners[key]);
}
}
@@ -38,7 +40,10 @@ var LongPollingBuffer = function (buffersize) {
while(data.length > this.size){
data.pop([]);
}
- notifyAll();
+ if (!dirty) {
+ process.nextTick(notifyAll);
+ dirty = true;
+ }
}
this.addListenerForUpdateSince = function(since, callback) {

0 comments on commit 6698dd4

Please sign in to comment.