Skip to content
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

Add getRemoteLimit to switch the limit by a route or identifies. #11

Open
wants to merge 1 commit 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
12 changes: 11 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,17 @@ function (req) {
}
```

A function that allows you to switch how rate identifies a route. By fefault, its the method:route_regex
A function that allows you to switch how rate identifies a route. By default, its the method:route_regex

- *getRemoteLimit*

```js
function (req) {
return {interval: 1, limit: 0};
}
```

A function that allows you to switch the limit by a route or identifies. By default, its `null`.

- *setHeaders*

Expand Down
23 changes: 18 additions & 5 deletions lib/rate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ exports.defaults = function () {
return req.route.method + ':' + req.route.regexp;
},

// function(req) {
// return {interval: 1, limit: 0}
// }
getRemoteLimit: null,

interval: 1,

Expand Down Expand Up @@ -82,22 +86,31 @@ exports.middleware = function recordRate(options) {

var routeKey = options.getRouteKey(req),
remoteKey = options.getRemoteKey(req),
limit = options.limit,
interval = options.interval,
incrementCallback = null;

// check if limit function is defined
if (typeof options.getRemoteLimit === 'function') {
var remoteLimit = options.getRemoteLimit(req);
limit = remoteLimit.limit;
interval = remoteLimit.interval;
}

// check if there is a limit set on this route, and reject request with headers if so
if (options.limit) {
if (limit) {

// use the callback to get the rate
incrementCallback = function (rate, resetTime) {

if (options.setHeaders) {
options.setHeadersHandler(req, res, rate, options.limit, resetTime);
options.setHeadersHandler(req, res, rate, limit, resetTime);
}

if (rate > options.limit) {
if (rate > limit) {

// we are officially over the limit
options.onLimitReached(req, res, rate, options.limit, resetTime, next);
options.onLimitReached(req, res, rate, limit, resetTime, next);

} else {

Expand All @@ -107,7 +120,7 @@ exports.middleware = function recordRate(options) {
};
}

options.handler.increment(routeKey, remoteKey, options, next, incrementCallback);
options.handler.increment(routeKey, remoteKey, {limit: limit, interval: interval}, next, incrementCallback);

// if we don't have rate limiting turned on, then we don't care when incrementing the rate finishes
// let's keep going with the request
Expand Down