1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-03 17:59:28 +02:00

Add rate limiting to the image processor.

This commit is contained in:
Simo Kinnunen 2014-05-21 14:32:21 +09:00
parent c0d02c4e3a
commit 98470cda20
3 changed files with 53 additions and 19 deletions

View file

@ -26,3 +26,25 @@ module.exports.validate = function(req, rules) {
}
})
}
module.exports.limit = function(limit, handler) {
var queue = []
var running = 0
function done() {
running -= 1
maybeNext()
}
function maybeNext() {
while (running < limit && queue.length) {
running += 1
handler.apply(null, queue.shift()).finally(done)
}
}
return function() {
queue.push(arguments)
maybeNext()
}
}