overleaf/services/web/app/src/infrastructure/RateLimiter.js
Jakob Ackermann 5e773ce950 Merge pull request #4101 from overleaf/ae-settings-module
Migrate from `settings-sharelatex` to `@overleaf/settings`

GitOrigin-RevId: 9a298ba26382180c1351683c5fddc9004418c1e6
2021-07-08 02:08:28 +00:00

42 lines
1.1 KiB
JavaScript

const settings = require('@overleaf/settings')
const Metrics = require('@overleaf/metrics')
const RedisWrapper = require('./RedisWrapper')
const rclient = RedisWrapper.client('ratelimiter')
const { RedisRateLimiter } = require('rolling-rate-limiter')
const { callbackify } = require('util')
async function addCount(opts) {
if (settings.disableRateLimits) {
return true
}
const namespace = `RateLimit:${opts.endpointName}:`
const k = `{${opts.subjectName}}`
const limiter = new RedisRateLimiter({
client: rclient,
namespace,
interval: opts.timeInterval * 1000,
maxInInterval: opts.throttle,
})
const rateLimited = await limiter.limit(k)
if (rateLimited) {
Metrics.inc('rate-limit-hit', 1, {
path: opts.endpointName,
})
}
return !rateLimited
}
async function clearRateLimit(endpointName, subject) {
// same as the key which will be built by RollingRateLimiter (namespace+k)
const keyName = `RateLimit:${endpointName}:{${subject}}`
await rclient.del(keyName)
}
module.exports = {
addCount: callbackify(addCount),
clearRateLimit: callbackify(clearRateLimit),
promises: {
addCount,
clearRateLimit,
},
}