enhance(backend): 設定ファイルでioredisの全てのオプションを指定可能に (#11329)

* enhance(backend): 設定ファイルでioredisの全てのオプションを指定可能に

* yappa kousuru

* fix

* fix?

* fix

* Revert "fix"

This reverts commit 227f19ff3a.

* fix
This commit is contained in:
tamaina 2023-07-20 19:50:31 +09:00 committed by GitHub
parent 47b684100d
commit 0a06eb27da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 66 deletions

View File

@ -82,6 +82,8 @@ redis:
#pass: example-pass #pass: example-pass
#prefix: example-prefix #prefix: example-prefix
#db: 1 #db: 1
# You can specify more ioredis options...
#username: example-username
#redisForPubsub: #redisForPubsub:
# host: localhost # host: localhost
@ -90,6 +92,8 @@ redis:
# #pass: example-pass # #pass: example-pass
# #prefix: example-prefix # #prefix: example-prefix
# #db: 1 # #db: 1
# # You can specify more ioredis options...
# #username: example-username
#redisForJobQueue: #redisForJobQueue:
# host: localhost # host: localhost
@ -98,6 +102,8 @@ redis:
# #pass: example-pass # #pass: example-pass
# #prefix: example-prefix # #prefix: example-prefix
# #db: 1 # #db: 1
# # You can specify more ioredis options...
# #username: example-username
# ┌───────────────────────────┐ # ┌───────────────────────────┐
#───┘ MeiliSearch configuration └───────────────────────────── #───┘ MeiliSearch configuration └─────────────────────────────

View File

@ -124,6 +124,7 @@
### Server ### Server
- bullをbull-mqにアップグレードし、ジョブキューのパフォーマンスを改善 - bullをbull-mqにアップグレードし、ジョブキューのパフォーマンスを改善
- ストリーミングのパフォーマンスを改善 - ストリーミングのパフォーマンスを改善
- 設定ファイルでioredisの全てのオプションを指定可能に
- Fix: 無効化されたアンテナにアクセスがあった際に再度有効化するように - Fix: 無効化されたアンテナにアクセスがあった際に再度有効化するように
- Fix: お知らせの画像URLを空にできない問題を修正 - Fix: お知らせの画像URLを空にできない問題を修正
- Fix: i/notificationsのsinceIdが機能しない問題を修正 - Fix: i/notificationsのsinceIdが機能しない問題を修正

View File

@ -2,14 +2,7 @@ import Redis from 'ioredis';
import { loadConfig } from './built/config.js'; import { loadConfig } from './built/config.js';
const config = loadConfig(); const config = loadConfig();
const redis = new Redis({ const redis = new Redis(config.redis);
port: config.redis.port,
host: config.redis.host,
family: config.redis.family == null ? 0 : config.redis.family,
password: config.redis.pass,
keyPrefix: `${config.redis.prefix}:`,
db: config.redis.db ?? 0,
});
redis.on('connect', () => redis.disconnect()); redis.on('connect', () => redis.disconnect());
redis.on('error', (e) => { redis.on('error', (e) => {

View File

@ -41,14 +41,7 @@ const $meilisearch: Provider = {
const $redis: Provider = { const $redis: Provider = {
provide: DI.redis, provide: DI.redis,
useFactory: (config: Config) => { useFactory: (config: Config) => {
return new Redis.Redis({ return new Redis.Redis(config.redis);
port: config.redis.port,
host: config.redis.host,
family: config.redis.family == null ? 0 : config.redis.family,
password: config.redis.pass,
keyPrefix: `${config.redis.prefix}:`,
db: config.redis.db ?? 0,
});
}, },
inject: [DI.config], inject: [DI.config],
}; };
@ -56,14 +49,7 @@ const $redis: Provider = {
const $redisForPub: Provider = { const $redisForPub: Provider = {
provide: DI.redisForPub, provide: DI.redisForPub,
useFactory: (config: Config) => { useFactory: (config: Config) => {
const redis = new Redis.Redis({ const redis = new Redis.Redis(config.redisForPubsub);
port: config.redisForPubsub.port,
host: config.redisForPubsub.host,
family: config.redisForPubsub.family == null ? 0 : config.redisForPubsub.family,
password: config.redisForPubsub.pass,
keyPrefix: `${config.redisForPubsub.prefix}:`,
db: config.redisForPubsub.db ?? 0,
});
return redis; return redis;
}, },
inject: [DI.config], inject: [DI.config],
@ -72,14 +58,7 @@ const $redisForPub: Provider = {
const $redisForSub: Provider = { const $redisForSub: Provider = {
provide: DI.redisForSub, provide: DI.redisForSub,
useFactory: (config: Config) => { useFactory: (config: Config) => {
const redis = new Redis.Redis({ const redis = new Redis.Redis(config.redisForPubsub);
port: config.redisForPubsub.port,
host: config.redisForPubsub.host,
family: config.redisForPubsub.family == null ? 0 : config.redisForPubsub.family,
password: config.redisForPubsub.pass,
keyPrefix: `${config.redisForPubsub.prefix}:`,
db: config.redisForPubsub.db ?? 0,
});
redis.subscribe(config.host); redis.subscribe(config.host);
return redis; return redis;
}, },

View File

@ -6,6 +6,16 @@ import * as fs from 'node:fs';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import { dirname, resolve } from 'node:path'; import { dirname, resolve } from 'node:path';
import * as yaml from 'js-yaml'; import * as yaml from 'js-yaml';
import type { RedisOptions } from 'ioredis';
type RedisOptionsSource = Partial<RedisOptions> & {
host: string;
port: number;
family?: number;
pass: string;
db?: number;
prefix?: string;
};
/** /**
* *
@ -35,30 +45,9 @@ export type Source = {
user: string; user: string;
pass: string; pass: string;
}[]; }[];
redis: { redis: RedisOptionsSource;
host: string; redisForPubsub?: RedisOptionsSource;
port: number; redisForJobQueue?: RedisOptionsSource;
family?: number;
pass: string;
db?: number;
prefix?: string;
};
redisForPubsub?: {
host: string;
port: number;
family?: number;
pass: string;
db?: number;
prefix?: string;
};
redisForJobQueue?: {
host: string;
port: number;
family?: number;
pass: string;
db?: number;
prefix?: string;
};
meilisearch?: { meilisearch?: {
host: string; host: string;
port: string; port: string;
@ -119,8 +108,9 @@ export type Mixin = {
mediaProxy: string; mediaProxy: string;
externalMediaProxyEnabled: boolean; externalMediaProxyEnabled: boolean;
videoThumbnailGenerator: string | null; videoThumbnailGenerator: string | null;
redisForPubsub: NonNullable<Source['redisForPubsub']>; redis: RedisOptions & RedisOptionsSource;
redisForJobQueue: NonNullable<Source['redisForJobQueue']>; redisForPubsub: RedisOptions & RedisOptionsSource;
redisForJobQueue: RedisOptions & RedisOptionsSource;
}; };
export type Config = Source & Mixin; export type Config = Source & Mixin;
@ -182,9 +172,9 @@ export function loadConfig() {
config.videoThumbnailGenerator.endsWith('/') ? config.videoThumbnailGenerator.substring(0, config.videoThumbnailGenerator.length - 1) : config.videoThumbnailGenerator config.videoThumbnailGenerator.endsWith('/') ? config.videoThumbnailGenerator.substring(0, config.videoThumbnailGenerator.length - 1) : config.videoThumbnailGenerator
: null; : null;
if (!config.redis.prefix) config.redis.prefix = mixin.host; mixin.redis = convertRedisOptions(config.redis, mixin.host);
if (config.redisForPubsub == null) config.redisForPubsub = config.redis; mixin.redisForPubsub = config.redisForPubsub ? convertRedisOptions(config.redisForPubsub, mixin.host) : mixin.redis;
if (config.redisForJobQueue == null) config.redisForJobQueue = config.redis; mixin.redisForJobQueue = config.redisForJobQueue ? convertRedisOptions(config.redisForJobQueue, mixin.host) : mixin.redis;
return Object.assign(config, mixin); return Object.assign(config, mixin);
} }
@ -196,3 +186,13 @@ function tryCreateUrl(url: string) {
throw new Error(`url="${url}" is not a valid URL.`); throw new Error(`url="${url}" is not a valid URL.`);
} }
} }
function convertRedisOptions(options: RedisOptionsSource, host: string): RedisOptions & RedisOptionsSource {
return {
...options,
prefix: options.prefix ?? host,
family: options.family == null ? 0 : options.family,
keyPrefix: `${options.prefix ?? host}:`,
db: options.db ?? 0,
};
}

View File

@ -15,11 +15,8 @@ export const QUEUE = {
export function baseQueueOptions(config: Config, queueName: typeof QUEUE[keyof typeof QUEUE]): Bull.QueueOptions { export function baseQueueOptions(config: Config, queueName: typeof QUEUE[keyof typeof QUEUE]): Bull.QueueOptions {
return { return {
connection: { connection: {
port: config.redisForJobQueue.port, ...config.redisForJobQueue,
host: config.redisForJobQueue.host, keyPrefix: undefined
family: config.redisForJobQueue.family == null ? 0 : config.redisForJobQueue.family,
password: config.redisForJobQueue.pass,
db: config.redisForJobQueue.db ?? 0,
}, },
prefix: config.redisForJobQueue.prefix ? `${config.redisForJobQueue.prefix}:queue:${queueName}` : `queue:${queueName}`, prefix: config.redisForJobQueue.prefix ? `${config.redisForJobQueue.prefix}:queue:${queueName}` : `queue:${queueName}`,
}; };