From 77f91d67b42ae50ba6176ac95247aad80db34811 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 12 Apr 2023 10:07:14 +0900 Subject: [PATCH] =?UTF-8?q?perf(backend):=20=E3=83=8E=E3=83=BC=E3=83=88?= =?UTF-8?q?=E4=BD=9C=E6=88=90=E6=99=82=E3=81=AE=E3=82=A2=E3=83=B3=E3=83=86?= =?UTF-8?q?=E3=83=8A=E8=BF=BD=E5=8A=A0=E3=83=91=E3=83=95=E3=82=A9=E3=83=BC?= =?UTF-8?q?=E3=83=9E=E3=83=B3=E3=82=B9=E3=82=92=E6=94=B9=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 11 ++++++++ packages/backend/src/core/AntennaService.ts | 26 +++++++++++++------ .../backend/src/core/NoteCreateService.ts | 9 +------ 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df2265727d..65b12e6fa2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,17 @@ --> +## 13.x.x (unreleased) + +### General +- + +### Client +- + +### Server +- ノート作成時のアンテナ追加パフォーマンスを改善 + ## 13.11.2 ### Note diff --git a/packages/backend/src/core/AntennaService.ts b/packages/backend/src/core/AntennaService.ts index 02e0b455fd..166c78f479 100644 --- a/packages/backend/src/core/AntennaService.ts +++ b/packages/backend/src/core/AntennaService.ts @@ -91,14 +91,24 @@ export class AntennaService implements OnApplicationShutdown { } @bindThis - public async addNoteToAntenna(antenna: Antenna, note: Note, noteUser: { id: User['id']; }): Promise { - this.redisClient.xadd( - `antennaTimeline:${antenna.id}`, - 'MAXLEN', '~', '200', - '*', - 'note', note.id); - - this.globalEventService.publishAntennaStream(antenna.id, 'note', note); + public async addNoteToAntennas(note: Note, noteUser: { id: User['id']; username: string; host: string | null; }): Promise { + const antennas = await this.getAntennas(); + const antennasWithMatchResult = await Promise.all(antennas.map(antenna => this.checkHitAntenna(antenna, note, noteUser).then(hit => [antenna, hit] as const))); + const matchedAntennas = antennasWithMatchResult.filter(([, hit]) => hit).map(([antenna]) => antenna); + + const redisPipeline = this.redisClient.pipeline(); + + for (const antenna of matchedAntennas) { + redisPipeline.xadd( + `antennaTimeline:${antenna.id}`, + 'MAXLEN', '~', '200', + '*', + 'note', note.id); + + this.globalEventService.publishAntennaStream(antenna.id, 'note', note); + } + + redisPipeline.exec(); } // NOTE: フォローしているユーザーのノート、リストのユーザーのノート、グループのユーザーのノート指定はパフォーマンス上の理由で無効になっている diff --git a/packages/backend/src/core/NoteCreateService.ts b/packages/backend/src/core/NoteCreateService.ts index fb7ee7080a..32e4fe7f8a 100644 --- a/packages/backend/src/core/NoteCreateService.ts +++ b/packages/backend/src/core/NoteCreateService.ts @@ -493,14 +493,7 @@ export class NoteCreateService implements OnApplicationShutdown { } }); - // Antenna - for (const antenna of (await this.antennaService.getAntennas())) { - this.antennaService.checkHitAntenna(antenna, note, user).then(hit => { - if (hit) { - this.antennaService.addNoteToAntenna(antenna, note, user); - } - }); - } + this.antennaService.addNoteToAntennas(note, user); if (data.reply) { this.saveReply(data.reply, note);