mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
28 lines
860 B
CoffeeScript
28 lines
860 B
CoffeeScript
settings = require("settings-sharelatex")
|
|
RedisWrapper = require('./RedisWrapper')
|
|
rclient = RedisWrapper.client('ratelimiter')
|
|
|
|
|
|
module.exports = RateLimiter =
|
|
|
|
_buildKey: (endpoint, subject) ->
|
|
return "RateLimiter:#{endpoint}:{#{subject}}"
|
|
|
|
addCount: (opts, callback = (opts, shouldProcess)->)->
|
|
k = RateLimiter._buildKey(opts.endpointName, opts.subjectName)
|
|
multi = rclient.multi()
|
|
multi.incr(k)
|
|
multi.get(k)
|
|
multi.expire(k, opts.timeInterval)
|
|
multi.exec (err, results)->
|
|
console.log ">> results", results
|
|
count = results[1]
|
|
# account for the different results from `multi` when using cluster
|
|
if count instanceof Object
|
|
count = count[1]
|
|
allow = count < opts.throttle
|
|
callback err, allow
|
|
|
|
clearRateLimit: (endpointName, subject, callback) ->
|
|
k = RateLimiter._buildKey(endpointName, subject)
|
|
rclient.del k, callback
|