From 0a06eb27da747c22ff8403ba9bf9f1704eb65425 Mon Sep 17 00:00:00 2001 From: tamaina Date: Thu, 20 Jul 2023 19:50:31 +0900 Subject: [PATCH] =?UTF-8?q?enhance(backend):=20=E8=A8=AD=E5=AE=9A=E3=83=95?= =?UTF-8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=A7ioredis=E3=81=AE=E5=85=A8?= =?UTF-8?q?=E3=81=A6=E3=81=AE=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=E3=82=92=E6=8C=87=E5=AE=9A=E5=8F=AF=E8=83=BD=E3=81=AB=20(#1132?= =?UTF-8?q?9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * enhance(backend): 設定ファイルでioredisの全てのオプションを指定可能に * yappa kousuru * fix * fix? * fix * Revert "fix" This reverts commit 227f19ff3afcbbd560b831493975206263a1a5a3. * fix --- .config/example.yml | 6 +++ CHANGELOG.md | 1 + packages/backend/check_connect.js | 9 +---- packages/backend/src/GlobalModule.ts | 27 ++----------- packages/backend/src/config.ts | 58 ++++++++++++++-------------- packages/backend/src/queue/const.ts | 7 +--- 6 files changed, 42 insertions(+), 66 deletions(-) diff --git a/.config/example.yml b/.config/example.yml index fcf22debc..b996a83fb 100644 --- a/.config/example.yml +++ b/.config/example.yml @@ -82,6 +82,8 @@ redis: #pass: example-pass #prefix: example-prefix #db: 1 + # You can specify more ioredis options... + #username: example-username #redisForPubsub: # host: localhost @@ -90,6 +92,8 @@ redis: # #pass: example-pass # #prefix: example-prefix # #db: 1 +# # You can specify more ioredis options... +# #username: example-username #redisForJobQueue: # host: localhost @@ -98,6 +102,8 @@ redis: # #pass: example-pass # #prefix: example-prefix # #db: 1 +# # You can specify more ioredis options... +# #username: example-username # ┌───────────────────────────┐ #───┘ MeiliSearch configuration └───────────────────────────── diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b4b2ad31..9ef1533ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -124,6 +124,7 @@ ### Server - bullをbull-mqにアップグレードし、ジョブキューのパフォーマンスを改善 - ストリーミングのパフォーマンスを改善 +- 設定ファイルでioredisの全てのオプションを指定可能に - Fix: 無効化されたアンテナにアクセスがあった際に再度有効化するように - Fix: お知らせの画像URLを空にできない問題を修正 - Fix: i/notificationsのsinceIdが機能しない問題を修正 diff --git a/packages/backend/check_connect.js b/packages/backend/check_connect.js index ef0a350fb..da78c8ec7 100644 --- a/packages/backend/check_connect.js +++ b/packages/backend/check_connect.js @@ -2,14 +2,7 @@ import Redis from 'ioredis'; import { loadConfig } from './built/config.js'; const config = loadConfig(); -const redis = new 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, -}); +const redis = new Redis(config.redis); redis.on('connect', () => redis.disconnect()); redis.on('error', (e) => { diff --git a/packages/backend/src/GlobalModule.ts b/packages/backend/src/GlobalModule.ts index 406e3192b..4caf4c3e9 100644 --- a/packages/backend/src/GlobalModule.ts +++ b/packages/backend/src/GlobalModule.ts @@ -41,14 +41,7 @@ const $meilisearch: Provider = { const $redis: Provider = { provide: DI.redis, useFactory: (config: Config) => { - return new Redis.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, - }); + return new Redis.Redis(config.redis); }, inject: [DI.config], }; @@ -56,14 +49,7 @@ const $redis: Provider = { const $redisForPub: Provider = { provide: DI.redisForPub, useFactory: (config: Config) => { - const redis = new Redis.Redis({ - 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, - }); + const redis = new Redis.Redis(config.redisForPubsub); return redis; }, inject: [DI.config], @@ -72,14 +58,7 @@ const $redisForPub: Provider = { const $redisForSub: Provider = { provide: DI.redisForSub, useFactory: (config: Config) => { - const redis = new Redis.Redis({ - 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, - }); + const redis = new Redis.Redis(config.redisForPubsub); redis.subscribe(config.host); return redis; }, diff --git a/packages/backend/src/config.ts b/packages/backend/src/config.ts index 8c312256d..bbacfe31a 100644 --- a/packages/backend/src/config.ts +++ b/packages/backend/src/config.ts @@ -6,6 +6,16 @@ import * as fs from 'node:fs'; import { fileURLToPath } from 'node:url'; import { dirname, resolve } from 'node:path'; import * as yaml from 'js-yaml'; +import type { RedisOptions } from 'ioredis'; + +type RedisOptionsSource = Partial & { + host: string; + port: number; + family?: number; + pass: string; + db?: number; + prefix?: string; +}; /** * ユーザーが設定する必要のある情報 @@ -35,30 +45,9 @@ export type Source = { user: string; pass: string; }[]; - redis: { - host: string; - port: number; - 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; - }; + redis: RedisOptionsSource; + redisForPubsub?: RedisOptionsSource; + redisForJobQueue?: RedisOptionsSource; meilisearch?: { host: string; port: string; @@ -119,8 +108,9 @@ export type Mixin = { mediaProxy: string; externalMediaProxyEnabled: boolean; videoThumbnailGenerator: string | null; - redisForPubsub: NonNullable; - redisForJobQueue: NonNullable; + redis: RedisOptions & RedisOptionsSource; + redisForPubsub: RedisOptions & RedisOptionsSource; + redisForJobQueue: RedisOptions & RedisOptionsSource; }; 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 : null; - if (!config.redis.prefix) config.redis.prefix = mixin.host; - if (config.redisForPubsub == null) config.redisForPubsub = config.redis; - if (config.redisForJobQueue == null) config.redisForJobQueue = config.redis; + mixin.redis = convertRedisOptions(config.redis, mixin.host); + mixin.redisForPubsub = config.redisForPubsub ? convertRedisOptions(config.redisForPubsub, mixin.host) : mixin.redis; + mixin.redisForJobQueue = config.redisForJobQueue ? convertRedisOptions(config.redisForJobQueue, mixin.host) : mixin.redis; return Object.assign(config, mixin); } @@ -196,3 +186,13 @@ function tryCreateUrl(url: string) { 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, + }; +} diff --git a/packages/backend/src/queue/const.ts b/packages/backend/src/queue/const.ts index d240fe70e..d49951a1c 100644 --- a/packages/backend/src/queue/const.ts +++ b/packages/backend/src/queue/const.ts @@ -15,11 +15,8 @@ export const QUEUE = { export function baseQueueOptions(config: Config, queueName: typeof QUEUE[keyof typeof QUEUE]): Bull.QueueOptions { return { connection: { - port: config.redisForJobQueue.port, - host: config.redisForJobQueue.host, - family: config.redisForJobQueue.family == null ? 0 : config.redisForJobQueue.family, - password: config.redisForJobQueue.pass, - db: config.redisForJobQueue.db ?? 0, + ...config.redisForJobQueue, + keyPrefix: undefined }, prefix: config.redisForJobQueue.prefix ? `${config.redisForJobQueue.prefix}:queue:${queueName}` : `queue:${queueName}`, };