Skip to content

Instantly share code, notes, and snippets.

@abrkn abrkn/gist:2688939
Created May 13, 2012

Embed
What would you like to do?
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
You can’t perform that action at this time.