merge: allow redis family to be specified as a string

Reviewed-on: FoundKeyGang/FoundKey#165
This commit is contained in:
Johann150 2022-10-02 18:46:34 +02:00
commit a7f9e244f3
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1
6 changed files with 45 additions and 33 deletions

View file

@ -57,7 +57,7 @@ db:
redis:
host: localhost
port: 6379
#family: 0 # 0=Both, 4=IPv4, 6=IPv6
#family: dual # can be either a number or string (0/dual, 4/ipv4, 6/ipv6)
#pass: example-pass
#prefix: example-prefix
#db: 1
@ -93,9 +93,6 @@ redis:
# deliverJobMaxAttempts: 12
# inboxJobMaxAttempts: 8
# IP address family used for outgoing requests (ipv4, ipv6 or dual)
#outgoingAddressFamily: ipv4
# Syslog option
#syslog:
# host: localhost

View file

@ -0,0 +1,34 @@
import Logger from '@/services/logger.js';
import config from './index.js';
const logger = new Logger('config:redis', 'gray', false);
function getRedisFamily(family?: string | 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 "dual"`);
}
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

@ -1,5 +1,5 @@
/**
*
* Configuration options set up by the user
*/
export type Source = {
repository_url?: string;
@ -19,7 +19,7 @@ export type Source = {
redis: {
host: string;
port: number;
family?: number;
family?: number | 'dual' | 'ipv4' | 'ipv6';
pass: string;
db?: number;
prefix?: string;
@ -47,8 +47,6 @@ export type Source = {
id: string;
outgoingAddressFamily?: 'ipv4' | 'ipv6' | 'dual';
deliverJobConcurrency?: number;
inboxJobConcurrency?: number;
deliverJobPerSec?: number;

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,15 +1,9 @@
import Redis from 'ioredis';
import config from '@/config/index.js';
import { getRedisOptions } from '@/config/redis.js';
export function createConnection() {
return new Redis({
port: config.redis.port,
host: config.redis.host,
family: config.redis.family ?? 0,
password: config.redis.pass,
keyPrefix: `${config.redis.prefix}:`,
db: config.redis.db || 0,
});
export function createConnection(): Redis.Redis {
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,