From 3cac8e0f6fb1a510a3f4dc712709a59da1561f8f Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 26 Mar 2022 17:43:08 +0900 Subject: [PATCH] perf(server): improve deliver performance --- .../src/remote/activitypub/deliver-manager.ts | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/backend/src/remote/activitypub/deliver-manager.ts b/packages/backend/src/remote/activitypub/deliver-manager.ts index 102f31931..9f21dc4cc 100644 --- a/packages/backend/src/remote/activitypub/deliver-manager.ts +++ b/packages/backend/src/remote/activitypub/deliver-manager.ts @@ -1,6 +1,7 @@ import { Users, Followings } from '@/models/index.js'; import { ILocalUser, IRemoteUser, User } from '@/models/entities/user.js'; import { deliver } from '@/queue/index.js'; +import { IsNull, Not } from 'typeorm'; //#region types interface IRecipe { @@ -82,15 +83,25 @@ export default class DeliverManager { for (const recipe of this.recipes) { if (isFollowers(recipe)) { // followers deliver - const followers = await Followings.findBy({ - followeeId: this.actor.id, - }); + // TODO: SELECT DISTINCT ON ("followerSharedInbox") "followerSharedInbox" みたいな問い合わせにすればよりパフォーマンス向上できそう + // ただ、sharedInboxがnullなリモートユーザーも稀におり、その対応ができなさそう? + const followers = await Followings.find({ + where: { + followeeId: this.actor.id, + followerHost: Not(IsNull()), + }, + select: { + followerSharedInbox: true, + followerInbox: true, + }, + }) as { + followerSharedInbox: string | null; + followerInbox: string; + }[]; for (const following of followers) { - if (Followings.isRemoteFollower(following)) { - const inbox = following.followerSharedInbox || following.followerInbox; - inboxes.add(inbox); - } + const inbox = following.followerSharedInbox || following.followerInbox; + inboxes.add(inbox); } } else if (isDirect(recipe)) { // direct deliver