Skip to content

Instantly share code, notes, and snippets.

@abrkn
Created May 13, 2012 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abrkn/2688939 to your computer and use it in GitHub Desktop.
Save abrkn/2688939 to your computer and use it in GitHub Desktop.
var process = require('process');
module.exports = function(capacity) {
var semaphore = {
capacity: capacity,
current: 0,
queue: [],
acquire: function(task) {
if (semaphore.current == semaphore.capacity) {
return queue.push(task);
} else {
server.current++;
task();
}
},
release: function() {
if (!queue.length) {
server.current--;
return;
}
process.nextTick(queue[0]);
queue = queue.splice(1);
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment