diff --git a/packages/backend/src/core/FederatedInstanceService.ts b/packages/backend/src/core/FederatedInstanceService.ts index 9e315e23f5..73bbf03b26 100644 --- a/packages/backend/src/core/FederatedInstanceService.ts +++ b/packages/backend/src/core/FederatedInstanceService.ts @@ -70,6 +70,24 @@ export class FederatedInstanceService implements OnApplicationShutdown { } } + @bindThis + public async fetch(host: string): Promise { + host = this.utilityService.toPuny(host); + + const cached = await this.federatedInstanceCache.get(host); + if (cached !== undefined) return cached; + + const index = await this.instancesRepository.findOneBy({ host }); + + if (index == null) { + this.federatedInstanceCache.set(host, null); + return null; + } else { + this.federatedInstanceCache.set(host, index); + return index; + } + } + @bindThis public async update(id: MiInstance['id'], data: Partial): Promise { const result = await this.instancesRepository.createQueryBuilder().update() diff --git a/packages/backend/src/queue/processors/DeliverProcessorService.ts b/packages/backend/src/queue/processors/DeliverProcessorService.ts index 1688b7836f..5a16496011 100644 --- a/packages/backend/src/queue/processors/DeliverProcessorService.ts +++ b/packages/backend/src/queue/processors/DeliverProcessorService.ts @@ -77,8 +77,14 @@ export class DeliverProcessorService { this.apRequestChart.deliverSucc(); this.federationChart.deliverd(host, true); - // Update stats - this.federatedInstanceService.fetchOrRegister(host).then(i => { + // Update instance stats + process.nextTick(async () => { + const i = await (this.meta.enableStatsForFederatedInstances + ? this.federatedInstanceService.fetchOrRegister(host) + : this.federatedInstanceService.fetch(host)); + + if (i == null) return; + if (i.isNotResponding) { this.federatedInstanceService.update(i.id, { isNotResponding: false, @@ -86,7 +92,9 @@ export class DeliverProcessorService { }); } - this.fetchInstanceMetadataService.fetchInstanceMetadata(i); + if (this.meta.enableStatsForFederatedInstances) { + this.fetchInstanceMetadataService.fetchInstanceMetadata(i); + } if (this.meta.enableChartsForFederatedInstances) { this.instanceChart.requestSent(i.host, true); @@ -98,7 +106,7 @@ export class DeliverProcessorService { this.apRequestChart.deliverFail(); this.federationChart.deliverd(host, false); - // Update stats + // Update instance stats this.federatedInstanceService.fetchOrRegister(host).then(i => { if (!i.isNotResponding) { this.federatedInstanceService.update(i.id, { diff --git a/packages/backend/src/queue/processors/InboxProcessorService.ts b/packages/backend/src/queue/processors/InboxProcessorService.ts index 591cf1a55d..95d764e4d8 100644 --- a/packages/backend/src/queue/processors/InboxProcessorService.ts +++ b/packages/backend/src/queue/processors/InboxProcessorService.ts @@ -195,21 +195,25 @@ export class InboxProcessorService implements OnApplicationShutdown { this.apRequestChart.inbox(); this.federationChart.inbox(authUser.user.host); - // Update stats - if (this.meta.enableStatsForFederatedInstances) { - this.federatedInstanceService.fetchOrRegister(authUser.user.host).then(i => { - this.updateInstanceQueue.enqueue(i.id, { - latestRequestReceivedAt: new Date(), - shouldUnsuspend: i.suspensionState === 'autoSuspendedForNotResponding', - }); + // Update instance stats + process.nextTick(async () => { + const i = await (this.meta.enableStatsForFederatedInstances + ? this.federatedInstanceService.fetchOrRegister(authUser.user.host) + : this.federatedInstanceService.fetch(authUser.user.host)); - if (this.meta.enableChartsForFederatedInstances) { - this.instanceChart.requestReceived(i.host); - } + if (i == null) return; - this.fetchInstanceMetadataService.fetchInstanceMetadata(i); + this.updateInstanceQueue.enqueue(i.id, { + latestRequestReceivedAt: new Date(), + shouldUnsuspend: i.suspensionState === 'autoSuspendedForNotResponding', }); - } + + if (this.meta.enableChartsForFederatedInstances) { + this.instanceChart.requestReceived(i.host); + } + + this.fetchInstanceMetadataService.fetchInstanceMetadata(i); + }); // アクティビティを処理 try {