mirror of
https://github.com/scratchfoundation/node-redis-rate-limiter.git
synced 2024-12-01 11:36:59 -05:00
27 lines
758 B
JavaScript
27 lines
758 B
JavaScript
var options = require('./options');
|
|
|
|
module.exports = function(opts) {
|
|
opts = options.canonical(opts);
|
|
return function(request, callback) {
|
|
var key = opts.key(request);
|
|
var tempKey = 'ratelimittemp:' + key;
|
|
var realKey = 'ratelimit:' + key;
|
|
opts.redis.multi()
|
|
.setex(tempKey, opts.window, 0)
|
|
.renamenx(tempKey, realKey)
|
|
.incr(realKey)
|
|
.exec(function(err, results) {
|
|
if(err) {
|
|
callback(err);
|
|
} else {
|
|
var current = results[2];
|
|
callback(null, {
|
|
current: current,
|
|
limit: opts.limit,
|
|
window: opts.window,
|
|
over: (current > opts.limit)
|
|
});
|
|
}
|
|
});
|
|
};
|
|
};
|