Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Limiter(opts) {
assert(this.db, '.db required');
this.max = opts.max || 2500;
this.duration = opts.duration || 3600000;
this.prefix = 'limit:' + this.id + ':';
this.key = 'limit:' + this.id;
}

/**
Expand All @@ -46,21 +46,17 @@ Limiter.prototype.inspect = function () {
/**
* Get values and header / status code and invoke `fn(err, info)`.
*
* redis is populated with the following keys
* redis is populated with the following key
* that expire after N seconds:
*
* - limit:<id>:count
* - limit:<id>:limit
* - limit:<id>:reset
* - limit:<id> (count, limit, reset)
*
* @param {Function} fn
* @api public
*/

Limiter.prototype.get = function (fn) {
var count = this.prefix + 'count';
var limit = this.prefix + 'limit';
var reset = this.prefix + 'reset';
var key = this.key;
var duration = this.duration;
var max = this.max;
var db = this.db;
Expand All @@ -69,9 +65,10 @@ Limiter.prototype.get = function (fn) {
var ex = (Date.now() + duration) / 1000 | 0;

db.multi()
.set([count, max, 'PX', duration, 'NX'])
.set([limit, max, 'PX', duration, 'NX'])
.set([reset, ex, 'PX', duration, 'NX'])
.hsetnx([key, 'count', max])
.hsetnx([key, 'limit', max])
.hsetnx([key, 'reset', ex])
.pexpire([key, duration])
.exec(function (err, res) {
if (err) return fn(err);

Expand All @@ -88,42 +85,43 @@ Limiter.prototype.get = function (fn) {
}

function decr(res) {
var n = ~~res[0];
var max = ~~res[1];
var ex = ~~res[2];
var dateNow = Date.now();
var n = parseInt(res.count);
var max = parseInt(res.limit);
var ex = parseInt(res.reset);

if (n <= 0) return done();
if (n === 0) return done(0);

function done() {
function done(n) {
fn(null, {
total: max,
remaining: n < 0 ? 0 : n,
remaining: n,
reset: ex
});
}

// setTimeout(function() {
db.multi()
.set([count, n - 1, 'PX', ex * 1000 - dateNow, 'XX'])
.pexpire([limit, ex * 1000 - dateNow])
.pexpire([reset, ex * 1000 - dateNow])
.hincrby([key, 'count', -1])
.pexpire([key, ex * 1000 - Date.now()])
.exec(function (err, res) {
if (err) return fn(err);
if (isFirstReplyNull(res)) return mget();
n = n - 1;
done();
done(n - 1);
});
// }, 1000)
}

function mget() {
db.watch([count], function (err) {
db.watch([key], function (err) {
if (err) return fn(err);
db.mget([count, limit, reset], function (err, res) {
db.persist([key], function (err, res) {
if (err) return fn(err);
if (!res[0] && res[0] !== 0) return create();

decr(res);
});
if (res === 0) return create();
db.hgetall([key], function (err, res) {
if (err) return fn(err);
decr(res);
});
})
});
}

Expand All @@ -149,4 +147,4 @@ function isFirstReplyNull(replies) {
!replies[0][1] :
// node_redis
!replies[0];
}
}
18 changes: 18 additions & 0 deletions index2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const redis = require('ioredis');
const sleep = require('sleep-promise');

const db1 = redis.createClient();
const db2 = redis.createClient();

(async () => {

for (let n=0; n < 100; n++) {
console.log('-------------')
console.log(await db1.set(['a', 10, 'PX', 1, 'NX']));
// await sleep(1)
console.log(await db1.get(['a']));
}
db1.quit();
db2.quit();
})();

4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var Limiter = require('..');
it('should reset', function(done) {
this.timeout(5000);
var limit = new Limiter({
duration: 2000,
duration: 500,
max: 2,
id: 'something',
db: db
Expand All @@ -110,7 +110,7 @@ var Limiter = require('..');
res.remaining.should.equal(2);
done();
});
}, 3000);
}, 1000);
});
});
});
Expand Down
Loading