2019-05-29 05:21:06 -04:00
|
|
|
const settings = require('settings-sharelatex')
|
|
|
|
const Metrics = require('metrics-sharelatex')
|
|
|
|
const RedisWrapper = require('./RedisWrapper')
|
|
|
|
const rclient = RedisWrapper.client('ratelimiter')
|
|
|
|
const RollingRateLimiter = require('rolling-rate-limiter')
|
2019-10-15 09:12:11 -04:00
|
|
|
const { promisifyAll } = require('../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-10-15 09:12:11 -04:00
|
|
|
const RateLimiter = {
|
2019-05-29 05:21:06 -04:00
|
|
|
addCount(opts, callback) {
|
2019-09-10 05:52:41 -04:00
|
|
|
if (settings.disableRateLimits) {
|
|
|
|
return callback(null, true)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
if (callback == null) {
|
2019-11-20 15:28:42 -05:00
|
|
|
callback = function() {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const namespace = `RateLimit:${opts.endpointName}:`
|
|
|
|
const k = `{${opts.subjectName}}`
|
|
|
|
const limiter = RollingRateLimiter({
|
|
|
|
redis: rclient,
|
|
|
|
namespace,
|
|
|
|
interval: opts.timeInterval * 1000,
|
|
|
|
maxInInterval: opts.throttle
|
|
|
|
})
|
2019-07-02 05:05:08 -04:00
|
|
|
limiter(k, function(err, timeLeft, actionsLeft) {
|
2019-11-20 15:28:42 -05:00
|
|
|
if (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
const allowed = timeLeft === 0
|
|
|
|
if (!allowed) {
|
|
|
|
Metrics.inc(`rate-limit-hit.${opts.endpointName}`, 1, {
|
|
|
|
path: opts.endpointName
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return callback(null, allowed)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
clearRateLimit(endpointName, subject, callback) {
|
|
|
|
// same as the key which will be built by RollingRateLimiter (namespace+k)
|
|
|
|
const keyName = `RateLimit:${endpointName}:{${subject}}`
|
2019-07-02 05:05:08 -04:00
|
|
|
rclient.del(keyName, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2019-10-15 09:12:11 -04:00
|
|
|
|
|
|
|
RateLimiter.promises = promisifyAll(RateLimiter)
|
|
|
|
module.exports = RateLimiter
|