diff --git a/node_modules/c9/ratelimit.js b/node_modules/c9/ratelimit.js index 404c6dfa..01c5cd65 100644 --- a/node_modules/c9/ratelimit.js +++ b/node_modules/c9/ratelimit.js @@ -6,26 +6,24 @@ var error = require("http-error"); module.exports = ratelimit; function ratelimit(key, duration, max) { - var limit = {}; + var counts = {}; return function(req, res, next) { var handle = req.params[key]; - var lim = limit[handle] || (limit[handle] = []); - var now = Date.now(); - for (var i = 0; i < lim.length; i++) { - if (now - lim[i] > duration) { - lim.splice(i, 1); - i--; - } - else break; - } - if (lim.length > max) { + counts[handle] = counts[handle] || 0; + if (counts[handle] >= max) { var err = new error.TooManyRequests("Rate limit exceeded"); - err.retryIn = duration - (Date.now() - lim[0]); - next(err); - return; + err.retryIn = Math.min(duration, 5000); + return next(err); } - lim.push(Date.now()); + + counts[handle]++; + setTimeout(function () { + counts[handle]--; + if (counts[handle] == 0) { + delete counts[handle]; + } + }, duration); return next(); };