mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-17 23:53:25 +02:00
Add rate limiting to the image processor.
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user