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 { 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,

View file

@ -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();

View file

@ -1,15 +1,10 @@
import Bull from 'bull';
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, {
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,