Meteor - How to implement a FIFO blocking queue?


#1

My application requires a server side FIFO blocking queue with blocking read with timeout (something equivalent to java.util.concurrent.LinkedBlockingQueue). My search in Meteor or other forums has not resulted in anything. Any suggestions will be really appreciated.


#2

Please be more specific about your use case. There are a number of ways to implement this from a simple javascript queue that wraps every passed function with a call to process the next item all the way to a web worker.

Keep in mind that Meteor (and all nodejs applications) are actually single threaded, so no locking or synchronisation constructs are actually needed. If you need it to be truly blocking, then you’d utilize fibers which is something that could be demonstrated.

One example of a simple queue:

function processQueue(){
	if (!this || !(this instanceof processQueue)){
		return new processQueue();
	}
	this.queue = [];
}
processQueue.prototype = {
	dequeue: function(){
		return this.queue.shift();
	},
	process: function(){
		this.dequeue()();
	},
	wrap: function(_func){
		var self = this;
		return new function(){
			_func();
			self.processAsync();
		}
	},
	enqueue: function(_func){
		this.queue.push(this.wrap(_func));
	},
	processAsync: function(){
		var self = this;
		Meteor.defer(function(){
			self.process();
		});
	}
}

With a little more information, exactly what you need could be engineered.