backend: refactor Redis option loading

This commit is contained in:
Norm 2022-09-21 09:55:05 -04:00
parent 5a52532c99
commit 508748ac0d
Signed by untrusted user: norm
GPG key ID: 7123E30E441E80DE
4 changed files with 42 additions and 34 deletions

View file

@ -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<string, string | number | undefined> {
return {
port: config.redis.port,
host: config.redis.host,
family: getRedisFamily(config.redis.family),
password: config.redis.pass,
db: config.redis.db || 0,
keyPrefix,
};
}

View file

@ -68,6 +68,7 @@ import { UserPending } from '@/models/entities/user-pending.js';
import { entities as charts } from '@/services/chart/entities.js'; import { entities as charts } from '@/services/chart/entities.js';
import { Webhook } from '@/models/entities/webhook.js'; import { Webhook } from '@/models/entities/webhook.js';
import { getRedisOptions } from '@/config/redis.js';
import { dbLogger } from './logger.js'; import { dbLogger } from './logger.js';
import { redisClient } from './redis.js'; import { redisClient } from './redis.js';
@ -186,14 +187,7 @@ export const db = new DataSource({
dropSchema: process.env.NODE_ENV === 'test', dropSchema: process.env.NODE_ENV === 'test',
cache: !config.db.disableCache ? { cache: !config.db.disableCache ? {
type: 'ioredis', type: 'ioredis',
options: { options: getRedisOptions(`${config.redis.prefix}:query:`),
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,
},
} : false, } : false,
logging: log, logging: log,
logger: log ? new MyCustomLogger() : undefined, logger: log ? new MyCustomLogger() : undefined,

View file

@ -1,25 +1,9 @@
import Redis from 'ioredis'; import Redis from 'ioredis';
import config from '@/config/index.js'; import config from '@/config/index.js';
import { IpFamily } from '@/config/types.js'; import { getRedisOptions } from '@/config/redis.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;
}
}
export function createConnection(): Redis.Redis { export function createConnection(): Redis.Redis {
return new Redis({ return new Redis(getRedisOptions(`${config.redis.prefix}:`));
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,
});
} }
export const subscriber = createConnection(); export const subscriber = createConnection();

View file

@ -1,15 +1,10 @@
import Bull from 'bull'; import Bull from 'bull';
import config from '@/config/index.js'; import config from '@/config/index.js';
import { getRedisOptions } from '@/config/redis.js';
export function initialize<T>(name: string, limitPerSec = -1) { export function initialize<T>(name: string, limitPerSec = -1): Bull.Queue<T> {
return new Bull<T>(name, { return new Bull<T>(name, {
redis: { redis: getRedisOptions(),
port: config.redis.port,
host: config.redis.host,
family: config.redis.family ?? 0,
password: config.redis.pass,
db: config.redis.db || 0,
},
prefix: config.redis.prefix ? `${config.redis.prefix}:queue` : 'queue', prefix: config.redis.prefix ? `${config.redis.prefix}:queue` : 'queue',
limiter: limitPerSec > 0 ? { limiter: limitPerSec > 0 ? {
max: limitPerSec, max: limitPerSec,