Skip to content

Commit

Permalink
merge #9 (Update redis commands to use array notation) by @ruimarinho
Browse files Browse the repository at this point in the history
  • Loading branch information
Noam Shemesh committed Jan 10, 2015
2 parents fd69ec8 + 8e01ba9 commit 7759ab2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Rate limiter for Node.js backed by Redis.

## Release Notes
[v2.0.0](https://github.com/tj/node-ratelimiter/tree/v2.0.0) - **API CHANGE** - Change `remaining` to include current call instead of decreasing it. Decreasing caused an off-by-one problem and caller could not distinguish between last legit call and a rejected call.

## Requirements

- Redis 2.6.12+.
Expand Down
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ 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')
.set([count, max, 'PX', duration, 'NX'])
.set([limit, max, 'PX', duration, 'NX'])
.set([reset, ex, 'PX', duration, 'NX'])
.exec(function (err, res) {
if (err) return fn(err);
// If the request has failed, it means the values already
// If the request has failed, it means the values already
// exist in which case we need to get the latest values.
if (!res || !res[0]) return mget();

Expand Down Expand Up @@ -102,7 +102,7 @@ Limiter.prototype.get = function (fn) {
}

db.multi()
.set(count, n - 1, 'PX', ex * 1000 - Date.now(), 'XX')
.set([count, n - 1, 'PX', ex * 1000 - Date.now(), 'XX'])
.exec(function (err, res) {
if (err) return fn(err);
if (!res || !res[0]) return mget();
Expand All @@ -112,9 +112,9 @@ Limiter.prototype.get = function (fn) {
}

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

Expand Down

0 comments on commit 7759ab2

Please sign in to comment.