mirror of
https://github.com/overleaf/overleaf.git
synced 2025-02-17 05:54:30 +00:00
WIP: try switch to rolling rate limiter
This commit is contained in:
parent
822f76a883
commit
5c25d15a18
2 changed files with 21 additions and 18 deletions
|
@ -1,27 +1,29 @@
|
||||||
settings = require("settings-sharelatex")
|
settings = require("settings-sharelatex")
|
||||||
RedisWrapper = require('./RedisWrapper')
|
RedisWrapper = require('./RedisWrapper')
|
||||||
rclient = RedisWrapper.client('ratelimiter')
|
rclient = RedisWrapper.client('ratelimiter')
|
||||||
|
RollingRateLimiter = require('rolling-rate-limiter')
|
||||||
|
|
||||||
|
|
||||||
module.exports = RateLimiter =
|
module.exports = RateLimiter =
|
||||||
|
|
||||||
_buildKey: (endpoint, subject) ->
|
|
||||||
return "RateLimiter:#{endpoint}:{#{subject}}"
|
|
||||||
|
|
||||||
addCount: (opts, callback = (err, shouldProcess)->)->
|
addCount: (opts, callback = (err, shouldProcess)->)->
|
||||||
k = RateLimiter._buildKey(opts.endpointName, opts.subjectName)
|
console.log ">> opts", opts
|
||||||
multi = rclient.multi()
|
namespace = "RateLimit:#{opts.endpointName}:"
|
||||||
multi.incr(k)
|
k = "{#{opts.subjectName}}"
|
||||||
multi.get(k)
|
limiter = RollingRateLimiter({
|
||||||
multi.expire(k, opts.timeInterval)
|
redis: rclient,
|
||||||
multi.exec (err, results)->
|
namespace: namespace,
|
||||||
count = results[1]
|
interval: opts.timeInterval * 1000,
|
||||||
# account for the different results from `multi` when using cluster
|
maxInInterval: opts.throttle
|
||||||
if count instanceof Object
|
})
|
||||||
count = count[1]
|
limiter k, (err, timeLeft, actionsLeft) ->
|
||||||
allow = count < opts.throttle
|
if err?
|
||||||
callback err, allow
|
return callback(err)
|
||||||
|
allowed = timeLeft == 0
|
||||||
|
console.log ">> limit", namespace, k, timeLeft, actionsLeft, ", allowed", allowed
|
||||||
|
callback(null, allowed)
|
||||||
|
|
||||||
clearRateLimit: (endpointName, subject, callback) ->
|
clearRateLimit: (endpointName, subject, callback) ->
|
||||||
k = RateLimiter._buildKey(endpointName, subject)
|
# same as the key which will be built by RollingRateLimiter (namespace+k)
|
||||||
rclient.del k, callback
|
keyName = "RateLimit:#{endpointName}:{#{subject}}"
|
||||||
|
rclient.del keyName, callback
|
||||||
|
|
|
@ -63,7 +63,8 @@
|
||||||
"underscore": "1.6.0",
|
"underscore": "1.6.0",
|
||||||
"v8-profiler": "^5.2.3",
|
"v8-profiler": "^5.2.3",
|
||||||
"xml2js": "0.2.0",
|
"xml2js": "0.2.0",
|
||||||
"passport-saml": "^0.15.0"
|
"passport-saml": "^0.15.0",
|
||||||
|
"rolling-rate-limiter": "^0.1.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"bunyan": "0.22.1",
|
"bunyan": "0.22.1",
|
||||||
|
|
Loading…
Reference in a new issue