From df5c491f4c6561595b826b75f82826e9af0b04ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=8B=E3=81=A3=E3=81=93=E3=81=8B=E3=82=8A?= <67428053+kakkokari-gtyih@users.noreply.github.com> Date: Sun, 12 Jul 2026 12:08:59 +0900 Subject: [PATCH] =?UTF-8?q?enhance(backend):=20summaly=E3=81=AE=E7=B5=90?= =?UTF-8?q?=E6=9E=9C=E3=82=92=E3=82=AD=E3=83=A3=E3=83=83=E3=82=B7=E3=83=A5?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=20(#17367)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * enhance(backend): summalyの結果をキャッシュするように * fix * fix: unify memorykvcache * fix * refactor * fix * Update Changelog --- CHANGELOG.md | 1 + packages/backend/src/misc/cache.ts | 27 ++++++- .../src/server/web/UrlPreviewService.ts | 75 +++++++++++++------ packages/backend/test/unit/misc/cache.ts | 23 ++++++ 4 files changed, 100 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8e73fed58..cca052f102 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ - Enhance: Sentry バックエンドの自動計装を `sentryForBackend.disabledIntegrations` で個別に無効化できるように - Enhance: Node.js 22.23.0以降、24.17.0以降、26.4.0以降をサポートするように - Enhance: Docker Image の Node.js を 26.4.0 に、Debian を trixie (v13) に更新 +- Enhance: URLプレビューの結果を内部でキャッシュするように - Fix: `/stats` API のレスポンス型が正しくない問題を修正 - Fix: ハッシュタグに関連するデータを更新する際のエラーハンドリングを修正 - Fix: Sentry 使用環境下にて、Misskey が発行した SQL クエリが span に含まれない問題を修正 diff --git a/packages/backend/src/misc/cache.ts b/packages/backend/src/misc/cache.ts index f1eae582a8..936b049f00 100644 --- a/packages/backend/src/misc/cache.ts +++ b/packages/backend/src/misc/cache.ts @@ -204,14 +204,13 @@ export class RedisSingleCache { } } -// TODO: メモリ節約のためあまり参照されないキーを定期的に削除できるようにする? - export class MemoryKVCache { private readonly cache = new Map(); private readonly gcIntervalHandle = setInterval(() => this.gc(), 1000 * 60 * 3); // 3m constructor( private readonly lifetime: number, + private readonly limit: number = Infinity, ) {} @bindThis @@ -220,6 +219,25 @@ export class MemoryKVCache { * @deprecated これを直接呼び出すべきではない。InternalEventなどで変更を全てのプロセス/マシンに通知するべき */ public set(key: string, value: T): void { + if (this.limit <= 0) { + throw new Error('Limit must be greater than 0'); + } + + if (this.limit !== Infinity) { + this.gc(); + + // 挿入順を更新して LRU を保つため、同一キーは一度削除する + this.cache.delete(key); + + while (this.cache.size >= this.limit) { + const oldestKey = this.cache.keys().next().value; + if (oldestKey == null) { + throw new Error('Cache is empty but size exceeds the limit'); + } + this.cache.delete(oldestKey); + } + } + this.cache.set(key, { date: Date.now(), value, @@ -234,6 +252,11 @@ export class MemoryKVCache { this.cache.delete(key); return undefined; } + if (this.limit !== Infinity) { + // access 順を更新して LRU を保つ + this.cache.delete(key); + this.cache.set(key, cached); + } return cached.value; } diff --git a/packages/backend/src/server/web/UrlPreviewService.ts b/packages/backend/src/server/web/UrlPreviewService.ts index e5ba8c596c..cb36b960ec 100644 --- a/packages/backend/src/server/web/UrlPreviewService.ts +++ b/packages/backend/src/server/web/UrlPreviewService.ts @@ -3,13 +3,14 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { Inject, Injectable } from '@nestjs/common'; +import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common'; import type { SummalyResult } from '@misskey-dev/summaly'; import { DI } from '@/di-symbols.js'; import type { Config } from '@/config.js'; import { HttpRequestService } from '@/core/HttpRequestService.js'; import type Logger from '@/logger.js'; import { query } from '@/misc/prelude/url.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import { LoggerService } from '@/core/LoggerService.js'; import { UtilityService } from '@/core/UtilityService.js'; import { bindThis } from '@/decorators.js'; @@ -18,8 +19,9 @@ import { MiMeta } from '@/models/Meta.js'; import type { FastifyRequest, FastifyReply } from 'fastify'; @Injectable() -export class UrlPreviewService { +export class UrlPreviewService implements OnApplicationShutdown { private logger: Logger; + private summaryCache: MemoryKVCache; private readonly summalyDefaultUserAgent: string; constructor( @@ -34,6 +36,7 @@ export class UrlPreviewService { private loggerService: LoggerService, ) { this.logger = this.loggerService.getLogger('url-preview'); + this.summaryCache = new MemoryKVCache(1000 * 60 * 60, 100); // 1h, 100件 this.summalyDefaultUserAgent = `SummalyBot/${_SUMMALY_VERSION_} (${this.config.url}; +https://github.com/misskey-dev/summaly/blob/master/README.md)`; } @@ -80,20 +83,32 @@ export class UrlPreviewService { : `Getting preview of ${url}@${lang} ...`); try { - const summary = this.meta.urlPreviewSummaryProxyUrl - ? await this.fetchSummaryFromProxy(url, this.meta, lang) - : await this.fetchSummary(url, this.meta, lang); + const fetcher = async () => { + const result = await ( + this.meta.urlPreviewSummaryProxyUrl + ? this.fetchSummaryFromProxy(url, lang) + : this.fetchSummary(url, lang) + ); + + if (!result.url.startsWith('http://') && !result.url.startsWith('https://')) { + return undefined; + } + + if (result.player.url && !result.player.url.startsWith('http://') && !result.player.url.startsWith('https://')) { + return undefined; + } + + return result; + }; + + const summary = await this.summaryCache.fetchMaybe(`${url}@${lang ?? '_DEFAULT_'}`, fetcher); + + if (summary == null) { + throw new Error('Invalid summary'); + } this.logger.succ(`Got preview of ${url}: ${summary.title}`); - if (!(summary.url.startsWith('http://') || summary.url.startsWith('https://'))) { - throw new Error('unsupported schema included'); - } - - if (summary.player.url && !(summary.player.url.startsWith('http://') || summary.player.url.startsWith('https://'))) { - throw new Error('unsupported schema included'); - } - summary.icon = this.wrap(summary.icon); summary.thumbnail = this.wrap(summary.thumbnail); @@ -120,7 +135,8 @@ export class UrlPreviewService { } } - private async fetchSummary(url: string, meta: MiMeta, lang?: string): Promise { + @bindThis + private async fetchSummary(url: string, lang?: string): Promise { const { summaly } = await import('@misskey-dev/summaly'); return summaly(url, { @@ -130,25 +146,36 @@ export class UrlPreviewService { http: this.httpRequestService.httpAgent, https: this.httpRequestService.httpsAgent, }, - userAgent: meta.urlPreviewUserAgent ?? this.summalyDefaultUserAgent, - operationTimeout: meta.urlPreviewTimeout, - contentLengthLimit: meta.urlPreviewMaximumContentLength, - contentLengthRequired: meta.urlPreviewRequireContentLength, + userAgent: this.meta.urlPreviewUserAgent ?? this.summalyDefaultUserAgent, + operationTimeout: this.meta.urlPreviewTimeout, + contentLengthLimit: this.meta.urlPreviewMaximumContentLength, + contentLengthRequired: this.meta.urlPreviewRequireContentLength, }); } - private fetchSummaryFromProxy(url: string, meta: MiMeta, lang?: string): Promise { - const proxy = meta.urlPreviewSummaryProxyUrl!; + @bindThis + private fetchSummaryFromProxy(url: string, lang?: string): Promise { + const proxy = this.meta.urlPreviewSummaryProxyUrl!; const queryStr = query({ url: url, lang: lang ?? 'ja-JP', followRedirects: this.meta.urlPreviewAllowRedirect, - userAgent: meta.urlPreviewUserAgent ?? this.summalyDefaultUserAgent, - operationTimeout: meta.urlPreviewTimeout, - contentLengthLimit: meta.urlPreviewMaximumContentLength, - contentLengthRequired: meta.urlPreviewRequireContentLength, + userAgent: this.meta.urlPreviewUserAgent ?? this.summalyDefaultUserAgent, + operationTimeout: this.meta.urlPreviewTimeout, + contentLengthLimit: this.meta.urlPreviewMaximumContentLength, + contentLengthRequired: this.meta.urlPreviewRequireContentLength, }); return this.httpRequestService.getJson(`${proxy}?${queryStr}`, 'application/json, */*', undefined, true); } + + @bindThis + public dispose(): void { + this.summaryCache.dispose(); + } + + @bindThis + public onApplicationShutdown(): void { + this.dispose(); + } } diff --git a/packages/backend/test/unit/misc/cache.ts b/packages/backend/test/unit/misc/cache.ts index 297378304e..80085cf148 100644 --- a/packages/backend/test/unit/misc/cache.ts +++ b/packages/backend/test/unit/misc/cache.ts @@ -38,6 +38,29 @@ describe('misc:MemoryKVCache', () => { cache.dispose(); }); + test('keeps current behavior when limit is omitted', () => { + const cache = new MemoryKVCache(1000 * 60); + cache.set('a', 1); + cache.set('b', 2); + cache.set('c', 3); + expect(cache.get('a')).toBe(1); + expect(cache.get('b')).toBe(2); + expect(cache.get('c')).toBe(3); + cache.dispose(); + }); + + test('evicts least recently used entry when limit is reached', () => { + const cache = new MemoryKVCache(1000 * 60, 2); + cache.set('a', 1); + cache.set('b', 2); + expect(cache.get('a')).toBe(1); + cache.set('c', 3); + expect(cache.get('a')).toBe(1); + expect(cache.get('b')).toBeUndefined(); + expect(cache.get('c')).toBe(3); + cache.dispose(); + }); + describe('gc()', () => { test('removes expired entries', () => { const cache = new MemoryKVCache(1000);