Skip to content

Commit

Permalink
disabled zremrangebyrank by default
Browse files Browse the repository at this point in the history
  • Loading branch information
peng-huang-ch committed Mar 2, 2019
1 parent af76ed4 commit 2103dcc
Show file tree
Hide file tree
Showing 3 changed files with 300 additions and 9 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ limit.get(function(err, limit){
- `db` - redis connection instance
- `max` - max requests within `duration` [2500]
- `duration` - of limit in milliseconds [3600000]
- `tidy` - limit the records count, no greater than `max` [false]

# License

Expand Down
23 changes: 14 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = Limiter;
function Limiter(opts) {
this.id = opts.id;
this.db = opts.db;
this.tidy = opts.tidy || false;
assert(this.id, '.id required');
assert(this.db, '.db required');
this.max = opts.max || 2500;
Expand Down Expand Up @@ -58,20 +59,24 @@ Limiter.prototype.inspect = function() {

Limiter.prototype.get = function (fn) {
var db = this.db;
var tidy = this.key;
var duration = this.duration;
var key = this.key;
var max = this.max;
var now = microtime.now();
var start = now - duration * 1000;

db.multi()
.zremrangebyscore([key, 0, start])
.zcard([key])
.zadd([key, now, now])
.zrange([key, 0, 0])
.zrange([key, -max, -max])
.zremrangebyrank([key, 0, -(max + 1)])
.pexpire([key, duration])
var operations = [
['zremrangebyscore', key, 0, start],
['zcard', key],
['zadd', key, now, now],
['zrange', key, 0, 0],
['zrange', key, -max, -max],
['pexpire', key, duration],
]
if (tidy) {
operations.splice(5, 0, ['zremrangebyrank', key, 0, -(max + 1)])
}
db.multi(operations)
.exec(function (err, res) {
if (err) return fn(err);

Expand Down
Loading

0 comments on commit 2103dcc

Please sign in to comment.