redis keys in easier way

This commit is contained in:
Simon Martín
2018-02-20 17:19:50 +01:00
parent 6564ed69d8
commit faab174a79
3 changed files with 13 additions and 26 deletions

View File

@@ -17,26 +17,9 @@ const RATE_LIMIT_ENDPOINTS_GROUPS = {
ENDPOINT_13: 'named',
ENDPOINT_14: 'named-update',
ENDPOINT_15: 'named-delete',
ENDPOINT_16: 'named-options',
ENDPOINT_17: 'named-tiles'
};
/**
* The full key is: rate-limit:store:{user}:{endpoint}
* The value is a Redis hash:
* maxBurst (b): Integer (as string)
* countPerPeriod (c): Integer (as string)
* period (p): Integer (as string)
*/
const RATE_LIMIT_STORE_KEY = 'rate-limit:store:';
/**
* The full key is: rate-limit:status:{user}:{endpoint}
* This key is managed by redis-cell (CL.THROTTLE command)
*/
const RATE_LIMIT_STATUS_KEY = 'rate-limit:status:';
function rateLimitMiddleware(metadataBackend, endpointGroup = null) {
return function rateLimit(req, res, next) {
@@ -92,26 +75,30 @@ function rateLimitMiddleware(metadataBackend, endpointGroup = null) {
*/
function getEndpointGroup() {
// TODO: get endpoint from route path
return RATE_LIMIT_ENDPOINTS_GROUPS.ENDPOINT_8;
return null;
}
/**
* Returns Redis key where the limits are saved by user and endpoint
* The value is a Redis hash:
* maxBurst (b): Integer (as string)
* countPerPeriod (c): Integer (as string)
* period (p): Integer (as string)
* @param {string} user
* @param {string} endpointGroup
*/
function getStoreKey(user, endpointGroup) {
return RATE_LIMIT_STORE_KEY + user + ':' + endpointGroup;
return `rate-limit:store:${user}:${endpointGroup}`;
}
/**
* Returns Redis key where the current state of the limit by user and endpoint
* (so, the key where CL.THROTTLE works)
* This key is managed by redis-cell (CL.THROTTLE command)
* @param {string} user
* @param {string} endpointGroup
*/
function getStatusKey(user, endpointGroup) {
return RATE_LIMIT_STATUS_KEY + user + ':' + endpointGroup;
return `rate-limit:status:${user}:${endpointGroup}`;
}
function getLuaScript() {
@@ -132,4 +119,4 @@ function getLuaScript() {
module.exports.rateLimitMiddleware = rateLimitMiddleware;
module.exports.RATE_LIMIT_ENDPOINTS_GROUPS = RATE_LIMIT_ENDPOINTS_GROUPS;
module.exports.RATE_LIMIT_STORE_KEY = RATE_LIMIT_STORE_KEY;
module.exports.getStoreKey = getStoreKey;

View File

@@ -5,7 +5,7 @@ const TestClient = require('../support/test-client');
const redis = require('redis');
const {
RATE_LIMIT_ENDPOINTS_GROUPS,
RATE_LIMIT_STORE_KEY
getStoreKey
} = require('../../lib/cartodb/middleware/rate-limit');
@@ -72,7 +72,7 @@ function setLimit(count, period, burst) {
return;
}
const key = RATE_LIMIT_STORE_KEY + user + ':' + RATE_LIMIT_ENDPOINTS_GROUPS.ENDPOINT_1;
const key = getStoreKey(user, RATE_LIMIT_ENDPOINTS_GROUPS.ENDPOINT_1);
redisClient.hset(key, 'b', burst, 'c', count, 'p', period, () => {
keysToDelete.push(key);
});

View File

@@ -5,7 +5,7 @@ const cartodbRedis = require('cartodb-redis');
const {
rateLimitMiddleware,
RATE_LIMIT_ENDPOINTS_GROUPS,
RATE_LIMIT_STORE_KEY
getStoreKey
} = require('../../../lib/cartodb/middleware/rate-limit');
let redisClient;
@@ -19,7 +19,7 @@ function setLimit(count, period, burst) {
return;
}
const key = RATE_LIMIT_STORE_KEY + user + ':' + RATE_LIMIT_ENDPOINTS_GROUPS.ENDPOINT_8;
const key = getStoreKey(user, RATE_LIMIT_ENDPOINTS_GROUPS.ENDPOINT_8);
redisClient.hset(key, 'b', burst, 'c', count, 'p', period, function() {
keysToDelete.push(key);
});