Skip to content

Commit

Permalink
Modernize rate limiter (meteor#9604)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebakerckhof authored and benjamn committed Feb 8, 2018
1 parent 5527646 commit 640011a
Show file tree
Hide file tree
Showing 8 changed files with 630 additions and 648 deletions.
61 changes: 32 additions & 29 deletions packages/ddp-rate-limiter/ddp-rate-limiter-test-service.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { DDPRateLimiter } from 'meteor/ddp-rate-limiter';
import { RATE_LIMIT_NUM_CALLS, RATE_LIMIT_INTERVAL_TIME_MS } from './ddp-rate-limiter-tests-common';

Meteor.methods({
// Adds in a new rule with the specific intervalTime and connectionId as
// passed in to speed up testing & allow the rule to apply to the connection
// testing the rate limit.
addRuleToDDPRateLimiter: function () {
var connection = this.connection;
addRuleToDDPRateLimiter() {
const connection = this.connection;
connection.lastRateLimitEvent = connection.lastRateLimitEvent || {};
connection.lastMethodName = connection.lastMethodName || '';
// XXX In Javascript v8 engine, we are currently guaranteed the ordering of
Expand All @@ -12,71 +17,69 @@ Meteor.methods({
// test.
//
// This is important because we use `connection.lastMethodName` to
// ignore the "getLastRateLimitEvent" method so that it can return
// ignore the 'getLastRateLimitEvent' method so that it can return
// the actual last rate limit event rather than the one
// corresponding to the method call to "getLastRateLimitEvent".
// corresponding to the method call to 'getLastRateLimitEvent'.
this.ruleId = DDPRateLimiter.addRule({
name: function (name) {
name(name) {
connection.lastMethodName = name;
if (name !== 'getLastRateLimitEvent') {
connection.lastRateLimitEvent.name = name;
}
return name !== "a-method-that-is-not-rate-limited";
return name !== 'a-method-that-is-not-rate-limited';
},
userId: function (userId) {
userId(userId) {
connection.lastRateLimitEvent.userId = userId;
return true;
},
type: function (type) {
type(type) {
// Special check to return proper name since 'getLastRateLimitEvent'
// is another method call
if (connection.lastMethodName !== 'getLastRateLimitEvent'){
if (connection.lastMethodName !== 'getLastRateLimitEvent') {
connection.lastRateLimitEvent.type = type;
}
return true;
},
clientAddress: function (clientAddress) {
connection.lastRateLimitEvent.clientAddress = clientAddress
clientAddress(clientAddress) {
connection.lastRateLimitEvent.clientAddress = clientAddress;
return true;
},
connectionId: this.connection.id
}, RATE_LIMIT_NUM_CALLS, RATE_LIMIT_INTERVAL_TIME_MS, function(reply, ruleInput) {
if (connection.lastMethodName !== 'getLastRateLimitEvent'){
connectionId: this.connection.id,
}, RATE_LIMIT_NUM_CALLS, RATE_LIMIT_INTERVAL_TIME_MS, (reply, ruleInput) => {
if (connection.lastMethodName !== 'getLastRateLimitEvent') {
connection.lastRateLimitEvent.reply = reply;
connection.lastRateLimitEvent.ruleInput = ruleInput;
}
});

return this.ruleId;
},
getLastRateLimitEvent: function () {
getLastRateLimitEvent() {
return this.connection.lastRateLimitEvent;
},
// Server side method to remove rule from DDP Rate Limiter
removeRuleFromDDPRateLimiter: function (id) {
removeRuleFromDDPRateLimiter(id) {
return DDPRateLimiter.removeRule(id);
},
// Print all the server rules for debugging purposes.
printCurrentListOfRules: function () {
printCurrentListOfRules() {
console.log('Current list of rules :', DDPRateLimiter.printRules());
},
removeUserByUsername: function (username) {
Meteor.users.remove({username: username});
removeUserByUsername(username) {
Meteor.users.remove({ username });
},
dummyMethod: function () {
return "yup";
dummyMethod() {
return 'yup';
},
'a-method-that-is-not-rate-limited': function () {
return "not-rate-limited";
'a-method-that-is-not-rate-limited'() {
return 'not-rate-limited';
},
addDefaultAccountsRateLimitRule: function () {
addDefaultAccountsRateLimitRule() {
Accounts.addDefaultRateLimit();
},
removeDefaultAccountsRateLimitRule: function () {
removeDefaultAccountsRateLimitRule() {
return Accounts.removeDefaultRateLimit();
}
},
});

Meteor.publish("testSubscription", function () {
return [];
});
Meteor.publish('testSubscription', () => []);
4 changes: 2 additions & 2 deletions packages/ddp-rate-limiter/ddp-rate-limiter-tests-common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Common settings for DDPRateLimiter tests.
RATE_LIMIT_NUM_CALLS = 5;
RATE_LIMIT_INTERVAL_TIME_MS = 5000;
export const RATE_LIMIT_NUM_CALLS = 5;
export const RATE_LIMIT_INTERVAL_TIME_MS = 5000;
Loading

0 comments on commit 640011a

Please sign in to comment.