perf(backend): queue の delayed の件数が増えた際に deliver-delayed と inbox-delayed が返ってこなくなる問題を修正 (MisskeyIO#750)

(cherry picked from commit 3fdcf99011)
This commit is contained in:
riku6460 2024-10-18 22:35:33 +09:00 committed by kakkokari-gtyih
parent b1aac6acc3
commit d0e1796ae4
2 changed files with 36 additions and 16 deletions

View File

@ -7,6 +7,7 @@ import { URL } from 'node:url';
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js'; import { Endpoint } from '@/server/api/endpoint-base.js';
import type { DeliverQueue } from '@/core/QueueModule.js'; import type { DeliverQueue } from '@/core/QueueModule.js';
import { ApiLoggerService } from '@/server/api/ApiLoggerService.js';
export const meta = { export const meta = {
tags: ['admin'], tags: ['admin'],
@ -48,24 +49,33 @@ export const paramDef = {
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
constructor( constructor(
@Inject('queue:deliver') public deliverQueue: DeliverQueue, @Inject('queue:deliver') public deliverQueue: DeliverQueue,
private apiLoggerService: ApiLoggerService,
) { ) {
super(meta, paramDef, async (ps, me) => { super(meta, paramDef, async (ps, me) => {
const jobs = await this.deliverQueue.getJobs(['delayed']); const jobs = await this.deliverQueue.getJobs(['delayed']);
const res = [] as [string, number][]; const res = new Map<string, number>();
for (const job of jobs) { for (const job of jobs) {
const host = new URL(job.data.to).host; let host: string;
if (res.find(x => x[0] === host)) { try {
res.find(x => x[0] === host)![1]++; host = new URL(job.data.to).host;
} catch (e) {
this.apiLoggerService.logger.warn(`failed to parse url in ${job.id}: ${e}`);
this.apiLoggerService.logger.warn(`id: ${job.id}, data: ${JSON.stringify(job.data)}`);
continue;
}
const found = res.get(host);
if (found) {
res.set(host, found + 1);
} else { } else {
res.push([host, 1]); res.set(host, 1);
} }
} }
res.sort((a, b) => b[1] - a[1]); return Array.from(res.entries()).sort((a, b) => b[1] - a[1]);
return res;
}); });
} }
} }

View File

@ -7,6 +7,7 @@ import { URL } from 'node:url';
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js'; import { Endpoint } from '@/server/api/endpoint-base.js';
import type { InboxQueue } from '@/core/QueueModule.js'; import type { InboxQueue } from '@/core/QueueModule.js';
import { ApiLoggerService } from '@/server/api/ApiLoggerService.js';
export const meta = { export const meta = {
tags: ['admin'], tags: ['admin'],
@ -48,24 +49,33 @@ export const paramDef = {
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
constructor( constructor(
@Inject('queue:inbox') public inboxQueue: InboxQueue, @Inject('queue:inbox') public inboxQueue: InboxQueue,
private apiLoggerService: ApiLoggerService,
) { ) {
super(meta, paramDef, async (ps, me) => { super(meta, paramDef, async (ps, me) => {
const jobs = await this.inboxQueue.getJobs(['delayed']); const jobs = await this.inboxQueue.getJobs(['delayed']);
const res = [] as [string, number][]; const res = new Map<string, number>();
for (const job of jobs) { for (const job of jobs) {
const host = new URL(job.data.signature.keyId).host; let host: string;
if (res.find(x => x[0] === host)) { try {
res.find(x => x[0] === host)![1]++; host = new URL(job.data.signature.keyId).host;
} catch (e) {
this.apiLoggerService.logger.warn(`failed to parse url in ${job.id}: ${e}`);
this.apiLoggerService.logger.warn(`id: ${job.id}, data: ${JSON.stringify(job.data)}`);
continue;
}
const found = res.get(host);
if (found) {
res.set(host, found + 1);
} else { } else {
res.push([host, 1]); res.set(host, 1);
} }
} }
res.sort((a, b) => b[1] - a[1]); return Array.from(res.entries()).sort((a, b) => b[1] - a[1]);
return res;
}); });
} }
} }