From ff6d9d28604d173b57bdb26b94ccd16bd0f4c4ba Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 7 Apr 2023 11:20:14 +0900 Subject: [PATCH] =?UTF-8?q?feat(backend):=20=E3=82=A4=E3=83=99=E3=83=B3?= =?UTF-8?q?=E3=83=88=E7=94=A8Redis=E3=82=92=E5=88=A5=E3=82=B5=E3=83=BC?= =?UTF-8?q?=E3=83=90=E3=83=BC=E3=81=AB=E5=88=86=E9=9B=A2=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config/docker_example.yml | 8 ++++ .config/example.yml | 8 ++++ .devcontainer/devcontainer.yml | 8 ++++ CHANGELOG.md | 1 + chart/files/default.yml | 9 +++++ packages/backend/check_connect.js | 11 ++++- packages/backend/src/GlobalModule.ts | 40 ++++++++++++------- packages/backend/src/config.ts | 9 +++++ packages/backend/src/core/AntennaService.ts | 8 ++-- packages/backend/src/core/MetaService.ts | 8 ++-- packages/backend/src/core/RoleService.ts | 8 ++-- packages/backend/src/core/WebhookService.ts | 8 ++-- packages/backend/src/di-symbols.ts | 2 +- packages/backend/src/redis.ts | 13 ------ .../server/api/StreamingApiServerService.ts | 8 ++-- 15 files changed, 98 insertions(+), 51 deletions(-) delete mode 100644 packages/backend/src/redis.ts diff --git a/.config/docker_example.yml b/.config/docker_example.yml index f8124bc9d..13ecfac54 100644 --- a/.config/docker_example.yml +++ b/.config/docker_example.yml @@ -62,6 +62,14 @@ redis: #prefix: example-prefix #db: 1 +#redisForPubsub: +# host: redis +# port: 6379 +# #family: 0 # 0=Both, 4=IPv4, 6=IPv6 +# #pass: example-pass +# #prefix: example-prefix +# #db: 1 + # ┌─────────────────────────────┐ #───┘ Elasticsearch configuration └───────────────────────────── diff --git a/.config/example.yml b/.config/example.yml index 92b872662..fbdb7b024 100644 --- a/.config/example.yml +++ b/.config/example.yml @@ -62,6 +62,14 @@ redis: #prefix: example-prefix #db: 1 +#redisForPubsub: +# host: localhost +# port: 6379 +# #family: 0 # 0=Both, 4=IPv4, 6=IPv6 +# #pass: example-pass +# #prefix: example-prefix +# #db: 1 + # ┌─────────────────────────────┐ #───┘ Elasticsearch configuration └───────────────────────────── diff --git a/.devcontainer/devcontainer.yml b/.devcontainer/devcontainer.yml index 8a363a15d..4cc7ae3b5 100644 --- a/.devcontainer/devcontainer.yml +++ b/.devcontainer/devcontainer.yml @@ -62,6 +62,14 @@ redis: #prefix: example-prefix #db: 1 +#redisForPubsub: +# host: redis +# port: 6379 +# #family: 0 # 0=Both, 4=IPv4, 6=IPv6 +# #pass: example-pass +# #prefix: example-prefix +# #db: 1 + # ┌─────────────────────────────┐ #───┘ Elasticsearch configuration └───────────────────────────── diff --git a/CHANGELOG.md b/CHANGELOG.md index e52c4e0d0..28be3faff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ - 「UIのアニメーションを減らす」 (`reduceAnimation`) で猫耳を撫でられなくなります ### Server +- イベント用Redisを別サーバーに分離できるように - サーバーの全体的なパフォーマンスを向上 - ノート作成時のパフォーマンスを向上 - アンテナのタイムライン取得時のパフォーマンスを向上 diff --git a/chart/files/default.yml b/chart/files/default.yml index 4061ca3eb..afaf8a162 100644 --- a/chart/files/default.yml +++ b/chart/files/default.yml @@ -78,10 +78,19 @@ db: redis: host: localhost port: 6379 + #family: 0 # 0=Both, 4=IPv4, 6=IPv6 #pass: example-pass #prefix: example-prefix #db: 1 +#redisForPubsub: +# host: localhost +# port: 6379 +# #family: 0 # 0=Both, 4=IPv4, 6=IPv6 +# #pass: example-pass +# #prefix: example-prefix +# #db: 1 + # ┌─────────────────────────────┐ #───┘ Elasticsearch configuration └───────────────────────────── diff --git a/packages/backend/check_connect.js b/packages/backend/check_connect.js index ed429c025..ef0a350fb 100644 --- a/packages/backend/check_connect.js +++ b/packages/backend/check_connect.js @@ -1,8 +1,15 @@ +import Redis from 'ioredis'; import { loadConfig } from './built/config.js'; -import { createRedisConnection } from './built/redis.js'; const config = loadConfig(); -const redis = createRedisConnection(config); +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, +}); redis.on('connect', () => redis.disconnect()); redis.on('error', (e) => { diff --git a/packages/backend/src/GlobalModule.ts b/packages/backend/src/GlobalModule.ts index 801f1db74..cb713b25a 100644 --- a/packages/backend/src/GlobalModule.ts +++ b/packages/backend/src/GlobalModule.ts @@ -2,18 +2,15 @@ import { setTimeout } from 'node:timers/promises'; import { Global, Inject, Module } from '@nestjs/common'; import Redis from 'ioredis'; import { DataSource } from 'typeorm'; -import { createRedisConnection } from '@/redis.js'; import { DI } from './di-symbols.js'; import { loadConfig } from './config.js'; import { createPostgresDataSource } from './postgres.js'; import { RepositoryModule } from './models/RepositoryModule.js'; import type { Provider, OnApplicationShutdown } from '@nestjs/common'; -const config = loadConfig(); - const $config: Provider = { provide: DI.config, - useValue: config, + useValue: loadConfig(), }; const $db: Provider = { @@ -28,18 +25,31 @@ const $db: Provider = { const $redis: Provider = { provide: DI.redis, useFactory: (config) => { - const redisClient = createRedisConnection(config); - return redisClient; + return 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, + }); }, inject: [DI.config], }; -const $redisSubscriber: Provider = { - provide: DI.redisSubscriber, +const $redisForPubsub: Provider = { + provide: DI.redisForPubsub, useFactory: (config) => { - const redisSubscriber = createRedisConnection(config); - redisSubscriber.subscribe(config.host); - return redisSubscriber; + const redis = new 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, + }); + redis.subscribe(config.host); + return redis; }, inject: [DI.config], }; @@ -47,14 +57,14 @@ const $redisSubscriber: Provider = { @Global() @Module({ imports: [RepositoryModule], - providers: [$config, $db, $redis, $redisSubscriber], - exports: [$config, $db, $redis, $redisSubscriber, RepositoryModule], + providers: [$config, $db, $redis, $redisForPubsub], + exports: [$config, $db, $redis, $redisForPubsub, RepositoryModule], }) export class GlobalModule implements OnApplicationShutdown { constructor( @Inject(DI.db) private db: DataSource, @Inject(DI.redis) private redisClient: Redis.Redis, - @Inject(DI.redisSubscriber) private redisSubscriber: Redis.Redis, + @Inject(DI.redisForPubsub) private redisForPubsub: Redis.Redis, ) {} async onApplicationShutdown(signal: string): Promise { @@ -69,7 +79,7 @@ export class GlobalModule implements OnApplicationShutdown { await Promise.all([ this.db.destroy(), this.redisClient.disconnect(), - this.redisSubscriber.disconnect(), + this.redisForPubsub.disconnect(), ]); } } diff --git a/packages/backend/src/config.ts b/packages/backend/src/config.ts index 73f45e92e..e8554e5c8 100644 --- a/packages/backend/src/config.ts +++ b/packages/backend/src/config.ts @@ -33,6 +33,14 @@ export type Source = { db?: number; prefix?: string; }; + redisForPubsub?: { + host: string; + port: number; + family?: number; + pass: string; + db?: number; + prefix?: string; + }; elasticsearch: { host: string; port: number; @@ -151,6 +159,7 @@ export function loadConfig() { : null; if (!config.redis.prefix) config.redis.prefix = mixin.host; + if (config.redisForPubsub == null) config.redisForPubsub = config.redis; return Object.assign(config, mixin); } diff --git a/packages/backend/src/core/AntennaService.ts b/packages/backend/src/core/AntennaService.ts index 4bd3f39af..35266ac16 100644 --- a/packages/backend/src/core/AntennaService.ts +++ b/packages/backend/src/core/AntennaService.ts @@ -27,8 +27,8 @@ export class AntennaService implements OnApplicationShutdown { @Inject(DI.redis) private redisClient: Redis.Redis, - @Inject(DI.redisSubscriber) - private redisSubscriber: Redis.Redis, + @Inject(DI.redisForPubsub) + private redisForPubsub: Redis.Redis, @Inject(DI.mutingsRepository) private mutingsRepository: MutingsRepository, @@ -52,12 +52,12 @@ export class AntennaService implements OnApplicationShutdown { this.antennasFetched = false; this.antennas = []; - this.redisSubscriber.on('message', this.onRedisMessage); + this.redisForPubsub.on('message', this.onRedisMessage); } @bindThis public onApplicationShutdown(signal?: string | undefined) { - this.redisSubscriber.off('message', this.onRedisMessage); + this.redisForPubsub.off('message', this.onRedisMessage); } @bindThis diff --git a/packages/backend/src/core/MetaService.ts b/packages/backend/src/core/MetaService.ts index 4b792c083..2b6160c82 100644 --- a/packages/backend/src/core/MetaService.ts +++ b/packages/backend/src/core/MetaService.ts @@ -14,8 +14,8 @@ export class MetaService implements OnApplicationShutdown { private intervalId: NodeJS.Timer; constructor( - @Inject(DI.redisSubscriber) - private redisSubscriber: Redis.Redis, + @Inject(DI.redisForPubsub) + private redisForPubsub: Redis.Redis, @Inject(DI.db) private db: DataSource, @@ -33,7 +33,7 @@ export class MetaService implements OnApplicationShutdown { }, 1000 * 60 * 5); } - this.redisSubscriber.on('message', this.onMessage); + this.redisForPubsub.on('message', this.onMessage); } @bindThis @@ -122,6 +122,6 @@ export class MetaService implements OnApplicationShutdown { @bindThis public onApplicationShutdown(signal?: string | undefined) { clearInterval(this.intervalId); - this.redisSubscriber.off('message', this.onMessage); + this.redisForPubsub.off('message', this.onMessage); } } diff --git a/packages/backend/src/core/RoleService.ts b/packages/backend/src/core/RoleService.ts index 54e098ea5..3379f5af8 100644 --- a/packages/backend/src/core/RoleService.ts +++ b/packages/backend/src/core/RoleService.ts @@ -64,8 +64,8 @@ export class RoleService implements OnApplicationShutdown { public static NotAssignedError = class extends Error {}; constructor( - @Inject(DI.redisSubscriber) - private redisSubscriber: Redis.Redis, + @Inject(DI.redisForPubsub) + private redisForPubsub: Redis.Redis, @Inject(DI.usersRepository) private usersRepository: UsersRepository, @@ -87,7 +87,7 @@ export class RoleService implements OnApplicationShutdown { this.rolesCache = new MemorySingleCache(Infinity); this.roleAssignmentByUserIdCache = new MemoryKVCache(Infinity); - this.redisSubscriber.on('message', this.onMessage); + this.redisForPubsub.on('message', this.onMessage); } @bindThis @@ -400,6 +400,6 @@ export class RoleService implements OnApplicationShutdown { @bindThis public onApplicationShutdown(signal?: string | undefined) { - this.redisSubscriber.off('message', this.onMessage); + this.redisForPubsub.off('message', this.onMessage); } } diff --git a/packages/backend/src/core/WebhookService.ts b/packages/backend/src/core/WebhookService.ts index ac1e413de..85594f855 100644 --- a/packages/backend/src/core/WebhookService.ts +++ b/packages/backend/src/core/WebhookService.ts @@ -13,14 +13,14 @@ export class WebhookService implements OnApplicationShutdown { private webhooks: Webhook[] = []; constructor( - @Inject(DI.redisSubscriber) - private redisSubscriber: Redis.Redis, + @Inject(DI.redisForPubsub) + private redisForPubsub: Redis.Redis, @Inject(DI.webhooksRepository) private webhooksRepository: WebhooksRepository, ) { //this.onMessage = this.onMessage.bind(this); - this.redisSubscriber.on('message', this.onMessage); + this.redisForPubsub.on('message', this.onMessage); } @bindThis @@ -82,6 +82,6 @@ export class WebhookService implements OnApplicationShutdown { @bindThis public onApplicationShutdown(signal?: string | undefined) { - this.redisSubscriber.off('message', this.onMessage); + this.redisForPubsub.off('message', this.onMessage); } } diff --git a/packages/backend/src/di-symbols.ts b/packages/backend/src/di-symbols.ts index 56ce755a1..482e8f83e 100644 --- a/packages/backend/src/di-symbols.ts +++ b/packages/backend/src/di-symbols.ts @@ -2,7 +2,7 @@ export const DI = { config: Symbol('config'), db: Symbol('db'), redis: Symbol('redis'), - redisSubscriber: Symbol('redisSubscriber'), + redisForPubsub: Symbol('redisForPubsub'), //#region Repositories usersRepository: Symbol('usersRepository'), diff --git a/packages/backend/src/redis.ts b/packages/backend/src/redis.ts deleted file mode 100644 index 690f4715d..000000000 --- a/packages/backend/src/redis.ts +++ /dev/null @@ -1,13 +0,0 @@ -import Redis from 'ioredis'; -import { Config } from '@/config.js'; - -export function createRedisConnection(config: Config): Redis.Redis { - return 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, - }); -} diff --git a/packages/backend/src/server/api/StreamingApiServerService.ts b/packages/backend/src/server/api/StreamingApiServerService.ts index bd2d436a2..e0e5b71a8 100644 --- a/packages/backend/src/server/api/StreamingApiServerService.ts +++ b/packages/backend/src/server/api/StreamingApiServerService.ts @@ -22,8 +22,8 @@ export class StreamingApiServerService { @Inject(DI.config) private config: Config, - @Inject(DI.redisSubscriber) - private redisSubscriber: Redis.Redis, + @Inject(DI.redisForPubsub) + private redisForPubsub: Redis.Redis, @Inject(DI.usersRepository) private usersRepository: UsersRepository, @@ -81,7 +81,7 @@ export class StreamingApiServerService { ev.emit(parsed.channel, parsed.message); } - this.redisSubscriber.on('message', onRedisMessage); + this.redisForPubsub.on('message', onRedisMessage); const main = new MainStreamConnection( this.channelsService, @@ -111,7 +111,7 @@ export class StreamingApiServerService { connection.once('close', () => { ev.removeAllListeners(); main.dispose(); - this.redisSubscriber.off('message', onRedisMessage); + this.redisForPubsub.off('message', onRedisMessage); if (intervalId) clearInterval(intervalId); });