From 508748ac0d25e3e754693188af584f4de3ea21d2 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Wed, 21 Sep 2022 09:55:05 -0400 Subject: [PATCH] backend: refactor Redis option loading --- packages/backend/src/config/redis.ts | 35 ++++++++++++++++++++++++ packages/backend/src/db/postgre.ts | 10 ++----- packages/backend/src/db/redis.ts | 20 ++------------ packages/backend/src/queue/initialize.ts | 11 ++------ 4 files changed, 42 insertions(+), 34 deletions(-) create mode 100644 packages/backend/src/config/redis.ts diff --git a/packages/backend/src/config/redis.ts b/packages/backend/src/config/redis.ts new file mode 100644 index 000000000..fa4b7a2f5 --- /dev/null +++ b/packages/backend/src/config/redis.ts @@ -0,0 +1,35 @@ +import Logger from '@/services/logger.js'; +import { IpFamily } from './types.js'; +import config from './index.js'; + +const logger = new Logger('config:redis', 'gray', false); + +function getRedisFamily(family?: IpFamily | number): number { + const familyMap = { + ipv4: 4, + ipv6: 6, + dual: 0, + }; + if (typeof family === 'string' && family in familyMap) { + return familyMap[family]; + } else if (typeof family === 'number' && Object.values(familyMap).includes(family)) { + return family; + } + + if (typeof family !== 'undefined') { + logger.warn(`redis family "${family}" is invalid, defaulting to "both"`); + } + + return 0; +} + +export function getRedisOptions(keyPrefix?: string): Record { + return { + port: config.redis.port, + host: config.redis.host, + family: getRedisFamily(config.redis.family), + password: config.redis.pass, + db: config.redis.db || 0, + keyPrefix, + }; +} diff --git a/packages/backend/src/db/postgre.ts b/packages/backend/src/db/postgre.ts index 363671268..ec56fb54f 100644 --- a/packages/backend/src/db/postgre.ts +++ b/packages/backend/src/db/postgre.ts @@ -68,6 +68,7 @@ import { UserPending } from '@/models/entities/user-pending.js'; import { entities as charts } from '@/services/chart/entities.js'; import { Webhook } from '@/models/entities/webhook.js'; +import { getRedisOptions } from '@/config/redis.js'; import { dbLogger } from './logger.js'; import { redisClient } from './redis.js'; @@ -186,14 +187,7 @@ export const db = new DataSource({ dropSchema: process.env.NODE_ENV === 'test', cache: !config.db.disableCache ? { type: 'ioredis', - options: { - host: config.redis.host, - port: config.redis.port, - family: config.redis.family ?? 0, - password: config.redis.pass, - keyPrefix: `${config.redis.prefix}:query:`, - db: config.redis.db || 0, - }, + options: getRedisOptions(`${config.redis.prefix}:query:`), } : false, logging: log, logger: log ? new MyCustomLogger() : undefined, diff --git a/packages/backend/src/db/redis.ts b/packages/backend/src/db/redis.ts index e576f5e35..f4eadce58 100644 --- a/packages/backend/src/db/redis.ts +++ b/packages/backend/src/db/redis.ts @@ -1,25 +1,9 @@ import Redis from 'ioredis'; import config from '@/config/index.js'; -import { IpFamily } from '@/config/types.js'; - -function getRedisFamily(family?: IpFamily | number): number { - switch (family) { - case 'ipv4': return 4; - case 'ipv6': return 6; - case 'dual': return 0; - default: return family ?? 0; - } -} +import { getRedisOptions } from '@/config/redis.js'; export function createConnection(): Redis.Redis { - return new Redis({ - port: config.redis.port, - host: config.redis.host, - family: getRedisFamily(config.redis.family), - password: config.redis.pass, - keyPrefix: `${config.redis.prefix}:`, - db: config.redis.db || 0, - }); + return new Redis(getRedisOptions(`${config.redis.prefix}:`)); } export const subscriber = createConnection(); diff --git a/packages/backend/src/queue/initialize.ts b/packages/backend/src/queue/initialize.ts index 4f251388b..10f47de12 100644 --- a/packages/backend/src/queue/initialize.ts +++ b/packages/backend/src/queue/initialize.ts @@ -1,15 +1,10 @@ import Bull from 'bull'; import config from '@/config/index.js'; +import { getRedisOptions } from '@/config/redis.js'; -export function initialize(name: string, limitPerSec = -1) { +export function initialize(name: string, limitPerSec = -1): Bull.Queue { return new Bull(name, { - redis: { - port: config.redis.port, - host: config.redis.host, - family: config.redis.family ?? 0, - password: config.redis.pass, - db: config.redis.db || 0, - }, + redis: getRedisOptions(), prefix: config.redis.prefix ? `${config.redis.prefix}:queue` : 'queue', limiter: limitPerSec > 0 ? { max: limitPerSec,