From faab174a79fdefbe4f3072f9df6a4732dd575cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Tue, 20 Feb 2018 17:19:50 +0100 Subject: [PATCH] redis keys in easier way --- lib/cartodb/middleware/rate-limit.js | 31 ++++++++-------------------- test/acceptance/rate-limit.test.js | 4 ++-- test/unit/cartodb/rate-limit.test.js | 4 ++-- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/lib/cartodb/middleware/rate-limit.js b/lib/cartodb/middleware/rate-limit.js index c0574f54..11e6abf1 100644 --- a/lib/cartodb/middleware/rate-limit.js +++ b/lib/cartodb/middleware/rate-limit.js @@ -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; diff --git a/test/acceptance/rate-limit.test.js b/test/acceptance/rate-limit.test.js index af3379cb..0a7438a8 100644 --- a/test/acceptance/rate-limit.test.js +++ b/test/acceptance/rate-limit.test.js @@ -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); }); diff --git a/test/unit/cartodb/rate-limit.test.js b/test/unit/cartodb/rate-limit.test.js index d85aefef..c8cca68b 100644 --- a/test/unit/cartodb/rate-limit.test.js +++ b/test/unit/cartodb/rate-limit.test.js @@ -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); });