mirror of
https://github.com/openstf/stf
synced 2025-10-04 18:29:17 +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
|
@ -512,6 +512,9 @@ program
|
||||||
.option('-r, --storage-url <url>'
|
.option('-r, --storage-url <url>'
|
||||||
, 'URL to storage client'
|
, 'URL to storage client'
|
||||||
, String)
|
, String)
|
||||||
|
.option('-c, --concurrency <num>'
|
||||||
|
, 'maximum number of simultaneous transformations'
|
||||||
|
, Number)
|
||||||
.option('--cache-dir <dir>'
|
.option('--cache-dir <dir>'
|
||||||
, 'where to cache images'
|
, 'where to cache images'
|
||||||
, String
|
, String
|
||||||
|
@ -525,6 +528,7 @@ program
|
||||||
port: options.port
|
port: options.port
|
||||||
, storageUrl: options.storageUrl
|
, storageUrl: options.storageUrl
|
||||||
, cacheDir: options.cacheDir
|
, cacheDir: options.cacheDir
|
||||||
|
, concurrency: options.concurrency || os.cpus().length
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ var http = require('http')
|
||||||
var express = require('express')
|
var express = require('express')
|
||||||
|
|
||||||
var logger = require('../../../../util/logger')
|
var logger = require('../../../../util/logger')
|
||||||
|
var requtil = require('../../../../util/requtil')
|
||||||
|
|
||||||
var parseCrop = require('./param/crop')
|
var parseCrop = require('./param/crop')
|
||||||
var parseGravity = require('./param/gravity')
|
var parseGravity = require('./param/gravity')
|
||||||
|
@ -18,26 +19,33 @@ module.exports = function(options) {
|
||||||
app.set('case sensitive routing', true)
|
app.set('case sensitive routing', true)
|
||||||
app.set('trust proxy', true)
|
app.set('trust proxy', true)
|
||||||
|
|
||||||
app.get('/api/v1/s/image/:id/*', function(req, res) {
|
app.get(
|
||||||
get(req.url, options)
|
'/api/v1/s/image/:id/*'
|
||||||
.then(function(stream) {
|
, requtil.limit(options.concurrency, function(req, res) {
|
||||||
return transform(stream, {
|
return get(req.url, options)
|
||||||
crop: parseCrop(req.query.crop)
|
.then(function(stream) {
|
||||||
, gravity: parseGravity(req.query.gravity)
|
return transform(stream, {
|
||||||
})
|
crop: parseCrop(req.query.crop)
|
||||||
})
|
, gravity: parseGravity(req.query.gravity)
|
||||||
.then(function(out) {
|
|
||||||
res.status(200)
|
|
||||||
out.pipe(res)
|
|
||||||
})
|
|
||||||
.catch(function(err) {
|
|
||||||
log.error('Unable to transform resource "%s"', req.params.id, err.stack)
|
|
||||||
res.status(500)
|
|
||||||
.json({
|
|
||||||
success: false
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
.then(function(out) {
|
||||||
|
res.status(200)
|
||||||
|
out.pipe(res)
|
||||||
|
})
|
||||||
|
.catch(function(err) {
|
||||||
|
log.error(
|
||||||
|
'Unable to transform resource "%s"'
|
||||||
|
, req.params.id
|
||||||
|
, err.stack
|
||||||
|
)
|
||||||
|
res.status(500)
|
||||||
|
.json({
|
||||||
|
success: false
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
server.listen(options.port)
|
server.listen(options.port)
|
||||||
log.info('Listening on port %d', options.port)
|
log.info('Listening on port %d', options.port)
|
||||||
|
|
|
@ -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