From 8a4c959798e190af6721d4dc180d56d0d343325f Mon Sep 17 00:00:00 2001 From: sjahl <636687+sjahl@users.noreply.github.com> Date: Mon, 30 Oct 2023 17:57:03 -0400 Subject: [PATCH] Support mulpitle connection modes --- .../browser/base/api.deployment.yaml | 12 +++--- deploy/manifests/redis/redis-values.yaml | 1 + graphql-api/src/cache.ts | 38 +++++++++++++------ graphql-api/src/config.ts | 9 +++-- graphql-api/src/graphql/rate-limiting.ts | 20 +++++++--- 5 files changed, 53 insertions(+), 27 deletions(-) diff --git a/deploy/manifests/browser/base/api.deployment.yaml b/deploy/manifests/browser/base/api.deployment.yaml index f5d7d61fc..3f483d778 100644 --- a/deploy/manifests/browser/base/api.deployment.yaml +++ b/deploy/manifests/browser/base/api.deployment.yaml @@ -31,12 +31,12 @@ spec: secretKeyRef: name: gnomad-es-elastic-user # FIXME: This depends on using "gnomad" as the ES cluster name key: elastic - - name: READ_CACHE_REDIS_URL - value: redis - - name: WRITE_CACHE_REDIS_URL - value: redis - - name: RATE_LIMITER_REDIS_URL - value: redis + - name: REDIS_HOST + value: 'redis' + - name: REDIS_PORT + value: '26379' + - name: REDIS_USE_SENTINEL + value: 'true' - name: TRUST_PROXY valueFrom: configMapKeyRef: diff --git a/deploy/manifests/redis/redis-values.yaml b/deploy/manifests/redis/redis-values.yaml index 9b6e1692b..730991c5a 100644 --- a/deploy/manifests/redis/redis-values.yaml +++ b/deploy/manifests/redis/redis-values.yaml @@ -3,6 +3,7 @@ auth: enabled: false sentinel: enabled: true + masterSet: gnomad resources: requests: cpu: '500m' diff --git a/graphql-api/src/cache.ts b/graphql-api/src/cache.ts index 54e9864a3..be7fb2b64 100644 --- a/graphql-api/src/cache.ts +++ b/graphql-api/src/cache.ts @@ -9,19 +9,33 @@ let fetchCacheValue = () => Promise.resolve(null) let setCacheValue = () => Promise.resolve() let setCacheExpiration = () => Promise.resolve() -if (config.READ_CACHE_REDIS_URL) { - const readCacheDb = new Redis({ - sentinels: [{ host: config.READ_CACHE_REDIS_URL, port: 26379 }], - name: 'mymaster', - role: 'slave', - db: 1, - }) +if (config.REDIS_HOST) { + let readCacheDb + let writeCacheDb + if (config.REDIS_USE_SENTINEL) { + readCacheDb = new Redis({ + sentinels: [{ host: config.REDIS_HOST, port: config.REDIS_PORT }], + name: config.REDIS_GROUP_NAME, + role: 'slave', + db: 1, + }) - const writeCacheDb = new Redis({ - sentinels: [{ host: config.WRITE_CACHE_REDIS_URL, port: 26379 }], - name: 'mymaster', - db: 1, - }) + writeCacheDb = new Redis({ + sentinels: [{ host: config.REDIS_HOST, port: config.REDIS_PORT }], + name: config.REDIS_GROUP_NAME, + db: 1, + }) + } else { + readCacheDb = new Redis({ + host: config.REDIS_HOST, + db: 1, + }) + + writeCacheDb = new Redis({ + host: config.REDIS_HOST, + db: 1, + }) + } const withTimeout = (fn: any, timeout: any) => { return (...args: any[]) => diff --git a/graphql-api/src/config.ts b/graphql-api/src/config.ts index cae7f29ca..a42d11d4a 100644 --- a/graphql-api/src/config.ts +++ b/graphql-api/src/config.ts @@ -27,8 +27,10 @@ const config: Record = { ELASTICSEARCH_QUEUE_TIMEOUT: JSON.parse(env.ELASTICSEARCH_QUEUE_TIMEOUT || '30') * 1000, ELASTICSEARCH_REQUEST_TIMEOUT: JSON.parse(env.ELASTICSEARCH_REQUEST_TIMEOUT || '60') * 1000, // Cache - READ_CACHE_REDIS_URL: env.READ_CACHE_REDIS_URL, - WRITE_CACHE_REDIS_URL: env.WRITE_CACHE_REDIS_URL, + REDIS_GROUP_NAME: env.REDIS_GROUP_NAME || 'gnomad', + REDIS_HOST: env.REDIS_HOST, + REDIS_PORT: JSON.parse(env.REDIS_PORT || '6379'), + REDIS_USE_SENTINEL: env.REDIS_USE_SENTINEL, CACHE_REQUEST_TIMEOUT: JSON.parse(env.CACHE_REQUEST_TIMEOUT || '15') * 1000, // Web server PORT: JSON.parse(env.PORT || '8000'), @@ -39,10 +41,9 @@ const config: Record = { MAX_QUERY_COST: JSON.parse(env.MAX_QUERY_COST || '25'), MAX_QUERY_COST_PER_MINUTE: JSON.parse(env.MAX_QUERY_COST_PER_MINUTE || '100'), MAX_REQUESTS_PER_MINUTE: JSON.parse(env.MAX_REQUESTS_PER_MINUTE || '30'), - RATE_LIMITER_REDIS_URL: env.RATE_LIMITER_REDIS_URL, } -const requiredConfig = ['ELASTICSEARCH_URL', 'RATE_LIMITER_REDIS_URL'] +const requiredConfig = ['ELASTICSEARCH_URL'] for (const setting of requiredConfig) { if (!config[setting]) { diff --git a/graphql-api/src/graphql/rate-limiting.ts b/graphql-api/src/graphql/rate-limiting.ts index 2881f83ab..de5c0e5a7 100644 --- a/graphql-api/src/graphql/rate-limiting.ts +++ b/graphql-api/src/graphql/rate-limiting.ts @@ -3,11 +3,21 @@ import config from '../config' import { UserVisibleError } from '../errors' import logger from '../logger' -const rateLimitDb = new Redis({ - sentinels: [{ host: config.RATE_LIMITER_REDIS_URL, port: 26379 }], - name: 'mymaster', - db: 2, -}) +let rateLimitDb: Redis + +if (config.REDIS_USE_SENTINEL) { + rateLimitDb = new Redis({ + sentinels: [{ host: config.REDIS_HOST, port: config.REDIS_PORT }], + name: config.REDIS_GROUP_NAME, + db: 2, + }) +} else { + rateLimitDb = new Redis({ + host: config.REDIS_HOST, + port: config.REDIS_PORT, + db: 2, + }) +} const increaseRateLimitCounter = (key: any, value: any) => { return Promise.race([