Skip to content
This repository has been archived by the owner on Sep 25, 2019. It is now read-only.

Commit

Permalink
update(server): remote rate metering
Browse files Browse the repository at this point in the history
it relied on `express-rate` which hasn't been updated in 5 years and depends on deprecated functions in NodeJS
we will use Cloud Endpoints for rate metering instead

Closes #115
  • Loading branch information
Splaktar committed Mar 18, 2017
1 parent caeba27 commit 91d80ca
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 44 deletions.
43 changes: 0 additions & 43 deletions lib/controllers/api/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var express = require('express'),
rate = require('express-rate'),
config = require('../../config/config'),
mongoose = require('mongoose'),
SimpleApiKey = mongoose.model('SimpleApiKey'),
Expand All @@ -13,13 +12,9 @@ var express = require('express'),
module.exports = function (app) {
const EDGE_CACHE_MAX_AGE = 3600; // 1 hr
var versions = [];
var rateHandler;
var cacher;

utils.fixCacher(Cacher);

console.log('Using in-memory rate handler...');
rateHandler = new rate.Memory.MemoryRateHandler();
// In-Memory Cache
cacher = new Cacher();

Expand Down Expand Up @@ -66,43 +61,6 @@ module.exports = function (app) {
}
};

var rateMiddleware = function (req, res, next) {

var limit = 10000;

if (req.apikey) {
limit = 50000;
}

var rm = rate.middleware({
handler: rateHandler,
limit: limit,
interval: 86400,
setHeadersHandler: function (req, res, rate, limit, resetTime) {
var remaining = limit - rate;

if (remaining < 0) {
remaining = 0;
}
res.setHeader('X-RateLimit-Limit', limit);
res.setHeader('X-RateLimit-Remaining', remaining);
res.setHeader('X-RateLimit-Reset', resetTime);
},
onLimitReached: function (req, res, rate, limit, resetTime, next) { // jshint ignore:line
res.json(403, {error: 'Rate limit exceeded. Check headers for limit information.'});
},
getRouteKey: function (req) { // jshint ignore:line
return 'api';
},
getRemoteKey: function (req) {
return req.headers['x-client-ip'] || req.ip;
}
});

rm(req, res, next);
};


/**
* Enable Google Edge caching for our API
* @param req
Expand All @@ -123,7 +81,6 @@ module.exports = function (app) {
var impl = express();

impl.use(apiKeyMiddleware);
impl.use(rateMiddleware);
impl.use(analyticsMiddleware(version));
impl.use(edgeCache);

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"ejs": "0.8.4",
"errorhandler": "1.5.0",
"express": "4.15.2",
"express-rate": "0.0.1",
"express-session": "1.15.1",
"google-oauth-jwt": "0.1.7",
"googleapis": "0.8.0",
Expand Down

0 comments on commit 91d80ca

Please sign in to comment.