Skip to content

Allow incrementing by other than 1 #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ rate: '100/h'

*Note:* the rate is parsed ahead of time, so this notation doesn't affect performance.

### `incr`

How much to increment the usage for a given call. This is useful in scenarios where incrementing by
one is not the whole story. For example, for rate limiting computation time. Some operations take
longer than other so counting operations doesn't work. Rather, use `incr` to track and limit the
actual compute time.

You would typically specify a custom function:

```js
// rate-limit based on the cost of an operation
key: function(x) { return x.cost; }
```

## HTTP middleware

This package contains a pre-built middleware,
Expand Down
9 changes: 8 additions & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ var getLimit = function(opts){
return opts.limit;
};

var getWindow = function(opts){
var getIncr = function (opts) {
if(typeof opts.incr === 'function') return opts.incr;
return function () { return opts.incr || 1; };
};

var getWindow = function (opts) {
if(getRate(opts)) return moment.duration(1, getMatches(opts)[2]) / 1000;
if(typeof opts.window === 'function') return opts.window();
return opts.window;
Expand All @@ -38,6 +43,7 @@ var validate = function(opts){
assert.equal(typeof getKey(opts), 'function', 'Invalid key: ' + opts.key);
if(opts.rate) assert.ok(getMatches(opts), 'Invalid rate: ' + getRate(opts));
assert.equal(typeof getLimit(opts), 'number', 'Invalid limit: ' + getLimit(opts));
assert.equal(typeof getIncr(opts), 'function', 'Invalid incr: ' + opts.incr);
assert.equal(typeof getWindow(opts), 'number', 'Invalid window: ' + getWindow(opts));
assert.notEqual(getLimit(opts), 0, 'Invalid rate limit: ' + getRate(opts));
assert.notEqual(getWindow(opts), 0, 'Invalid rate window: ' + getRate(opts));
Expand All @@ -50,6 +56,7 @@ canonical = function(opts) {
key: getKey(opts),
rate: getRate.bind(null, opts),
limit: getLimit.bind(null, opts),
incr: getIncr(opts),
window: getWindow.bind(null, opts)
};
};
Expand Down
3 changes: 2 additions & 1 deletion lib/rate-limiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ module.exports = function(opts) {
opts = options.canonical(opts);
return function(request, callback) {
var key = opts.key(request);
var incr = opts.incr(request);
var tempKey = 'ratelimittemp:' + key;
var realKey = 'ratelimit:' + key;
opts.redis.multi()
.setex(tempKey, opts.window(), 0)
.renamenx(tempKey, realKey)
.incr(realKey)
.incrby(realKey, incr)
.ttl(realKey)
.exec(function(err, results) {
if(err) {
Expand Down