add rate limits for autocompiles

global rate limit for all users and a lower rate limit for free users
This commit is contained in:
Brian Gough 2017-10-03 16:16:21 +01:00
parent cab3efd77c
commit d14723f24a

View file

@ -34,12 +34,16 @@ module.exports = CompileManager =
return callback(error) if error? return callback(error) if error?
for key, value of limits for key, value of limits
options[key] = value options[key] = value
# only pass user_id down to clsi if this is a per-user compile # Put a lower limit on autocompiles for free users
compileAsUser = if Settings.disablePerUserCompiles then undefined else user_id CompileManager._checkIfFreeAutoCompileLimitHasBeenHit options.isAutoCompile, limits.compileGroup, (err, canCompile)->
ClsiManager.sendRequest project_id, compileAsUser, options, (error, status, outputFiles, clsiServerId, validationProblems) -> if !canCompile
return callback(error) if error? return callback null, "autocompile-backoff", []
logger.log files: outputFiles, "output files" # only pass user_id down to clsi if this is a per-user compile
callback(null, status, outputFiles, clsiServerId, limits, validationProblems) compileAsUser = if Settings.disablePerUserCompiles then undefined else user_id
ClsiManager.sendRequest project_id, compileAsUser, options, (error, status, outputFiles, clsiServerId, validationProblems) ->
return callback(error) if error?
logger.log files: outputFiles, "output files"
callback(null, status, outputFiles, clsiServerId, limits, validationProblems)
stopCompile: (project_id, user_id, callback = (error) ->) -> stopCompile: (project_id, user_id, callback = (error) ->) ->
@ -75,15 +79,41 @@ module.exports = CompileManager =
_checkIfAutoCompileLimitHasBeenHit: (isAutoCompile, callback = (err, canCompile)->)-> _checkIfAutoCompileLimitHasBeenHit: (isAutoCompile, callback = (err, canCompile)->)->
if !isAutoCompile if !isAutoCompile
return callback(null, true) return callback(null, true)
opts = Metrics.inc "auto-compile"
opts =
endpointName:"auto_compile" endpointName:"auto_compile"
timeInterval:20 timeInterval:20
subjectName:"everyone" subjectName:"everyone"
throttle: 25 throttle: 200
rateLimiter.addCount opts, (err, canCompile)-> rateLimiter.addCount opts, (err, canCompile)->
if err? if err?
canCompile = false canCompile = false
logger.log canCompile:canCompile, opts:opts, "checking if auto compile limit has been hit" logger.log canCompile:canCompile, opts:opts, "checking if auto compile limit has been hit"
if !canCompile
Metrics.inc "auto-compile-rate-limited"
callback err, canCompile
_checkIfFreeAutoCompileLimitHasBeenHit: (isAutoCompile, compileGroup, callback = (err, canCompile)->)->
if !isAutoCompile
return callback(null, true)
if compileGroup is "priority"
Metrics.inc "auto-compile-priority"
return callback(null, true)
Metrics.inc "auto-compile-free"
opts =
endpointName:"auto_compile"
timeInterval:20
subjectName:"free"
throttle: 100
rateLimiter.addCount opts, (err, canCompile)->
if err?
canCompile = false
logger.log canCompile:canCompile, opts:opts, "checking if free users auto compile limit has been hit"
if !canCompile
Metrics.inc "auto-compile-free-rate-limited"
callback err, canCompile callback err, canCompile
_ensureRootDocumentIsSet: (project_id, callback = (error) ->) -> _ensureRootDocumentIsSet: (project_id, callback = (error) ->) ->