mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-13 21:53:43 +01:00
Merge pull request #3967 from overleaf/jpa-upgrade-ratelimiter
[misc] upgrade rolling-rate-limiter to latest upstream version GitOrigin-RevId: cf2fdf9ea7dab1fb4953de3a4f03e4dc913bcb23
This commit is contained in:
@@ -2,45 +2,41 @@ const settings = require('settings-sharelatex')
|
||||
const Metrics = require('@overleaf/metrics')
|
||||
const RedisWrapper = require('./RedisWrapper')
|
||||
const rclient = RedisWrapper.client('ratelimiter')
|
||||
const RollingRateLimiter = require('rolling-rate-limiter')
|
||||
const { promisifyAll } = require('../util/promises')
|
||||
const { RedisRateLimiter } = require('rolling-rate-limiter')
|
||||
const { callbackify } = require('util')
|
||||
|
||||
const RateLimiter = {
|
||||
addCount(opts, callback) {
|
||||
if (settings.disableRateLimits) {
|
||||
return callback(null, true)
|
||||
}
|
||||
if (callback == null) {
|
||||
callback = function () {}
|
||||
}
|
||||
const namespace = `RateLimit:${opts.endpointName}:`
|
||||
const k = `{${opts.subjectName}}`
|
||||
const limiter = RollingRateLimiter({
|
||||
redis: rclient,
|
||||
namespace,
|
||||
interval: opts.timeInterval * 1000,
|
||||
maxInInterval: opts.throttle,
|
||||
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,
|
||||
})
|
||||
limiter(k, function (err, timeLeft, actionsLeft) {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
const allowed = timeLeft === 0
|
||||
if (!allowed) {
|
||||
Metrics.inc('rate-limit-hit', 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}}`
|
||||
rclient.del(keyName, callback)
|
||||
},
|
||||
}
|
||||
return !rateLimited
|
||||
}
|
||||
|
||||
RateLimiter.promises = promisifyAll(RateLimiter)
|
||||
module.exports = RateLimiter
|
||||
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,
|
||||
},
|
||||
}
|
||||
|
||||
Generated
+12
-3
@@ -30016,10 +30016,19 @@
|
||||
"integrity": "sha512-fJhQQI5tLrQvYIYFpOnFinzv9dwmR7hRnUz1XqP3OJ1jIweTNOd6aTO4jwQSgcBSFUB+/KHJxuGneime+FdzOw=="
|
||||
},
|
||||
"rolling-rate-limiter": {
|
||||
"version": "https://github.com/ShaneKilkelly/rolling-rate-limiter/archive/8a1a2cd8aaf9cd1a75cc81317b7f261157be2149.tar.gz",
|
||||
"integrity": "sha512-QjJZHl4u7S+89r5V9H3QJkBqWmhQPlmjaxiTNZb8yJVblELEKmrtk25tey6lQq3OJDAzZgYIHigYOGbMiJRgmA==",
|
||||
"version": "0.2.10",
|
||||
"resolved": "https://registry.npmjs.org/rolling-rate-limiter/-/rolling-rate-limiter-0.2.10.tgz",
|
||||
"integrity": "sha512-66c/RV07ZCdTNGfm1BK8PH4OKcZWGvRXt4GfJqex1WjHjL0lSKlO6fnH9lmONkDn3itE88t8m4lpnT1mE0ekQw==",
|
||||
"requires": {
|
||||
"microtime-nodejs": "~1.0.0"
|
||||
"microtime-nodejs": "~1.0.0",
|
||||
"uuid": "^8.3.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"uuid": {
|
||||
"version": "8.3.2",
|
||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
|
||||
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="
|
||||
}
|
||||
}
|
||||
},
|
||||
"route-recognizer": {
|
||||
|
||||
@@ -155,7 +155,7 @@
|
||||
"request-promise-native": "^1.0.8",
|
||||
"requestretry": "^1.13.0",
|
||||
"rimraf": "2.2.6",
|
||||
"rolling-rate-limiter": "https://github.com/ShaneKilkelly/rolling-rate-limiter/archive/8a1a2cd8aaf9cd1a75cc81317b7f261157be2149.tar.gz",
|
||||
"rolling-rate-limiter": "^0.2.10",
|
||||
"sanitize-html": "^1.27.1",
|
||||
"sanitizer": "0.1.1",
|
||||
"scroll-into-view-if-needed": "^2.2.25",
|
||||
|
||||
Reference in New Issue
Block a user