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:
parent
c0d02c4e3a
commit
98470cda20
3 changed files with 53 additions and 19 deletions
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue