Created
May 13, 2012 15:30
-
-
Save abrkn/2688939 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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