Skip to content

Commit

Permalink
Support mulpitle connection modes
Browse files Browse the repository at this point in the history
  • Loading branch information
sjahl committed Oct 31, 2023
1 parent c08d240 commit 8a4c959
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 27 deletions.
12 changes: 6 additions & 6 deletions deploy/manifests/browser/base/api.deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions deploy/manifests/redis/redis-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ auth:
enabled: false
sentinel:
enabled: true
masterSet: gnomad
resources:
requests:
cpu: '500m'
Expand Down
38 changes: 26 additions & 12 deletions graphql-api/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) =>
Expand Down
9 changes: 5 additions & 4 deletions graphql-api/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ const config: Record<string, any> = {
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'),
Expand All @@ -39,10 +41,9 @@ const config: Record<string, any> = {
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]) {
Expand Down
20 changes: 15 additions & 5 deletions graphql-api/src/graphql/rate-limiting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit 8a4c959

Please sign in to comment.