overleaf/services/web/app/coffee/infrastructure/RateLimiter.coffee

15 lines
565 B
CoffeeScript
Raw Normal View History

settings = require("settings-sharelatex")
2014-09-26 13:52:00 +00:00
redis = require("redis-sharelatex")
rclient = redis.createClient(settings.redis.web)
redback = require("redback").use(rclient)
module.exports =
addCount: (opts, callback = (opts, shouldProcess)->)->
ratelimit = redback.createRateLimit(opts.endpointName)
ratelimit.addCount opts.subjectName, opts.timeInterval, (err, callCount)->
shouldProcess = callCount < opts.throttle
2015-02-05 10:18:18 +00:00
callback(err, shouldProcess)
clearRateLimit: (endpointName, subject, callback) ->
rclient.del "#{endpointName}:#{subject}", callback