mirror of
https://github.com/misskey-dev/misskey.git
synced 2026-07-27 20:34:37 +02:00
enhance(backend): summalyの結果をキャッシュするように (#17367)
* enhance(backend): summalyの結果をキャッシュするように * fix * fix: unify memorykvcache * fix * refactor * fix * Update Changelog
This commit is contained in:
@@ -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 に含まれない問題を修正
|
||||
|
||||
@@ -204,14 +204,13 @@ export class RedisSingleCache<T> {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: メモリ節約のためあまり参照されないキーを定期的に削除できるようにする?
|
||||
|
||||
export class MemoryKVCache<T> {
|
||||
private readonly cache = new Map<string, { date: number; value: T; }>();
|
||||
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<T> {
|
||||
* @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<T> {
|
||||
this.cache.delete(key);
|
||||
return undefined;
|
||||
}
|
||||
if (this.limit !== Infinity) {
|
||||
// access 順を更新して LRU を保つ
|
||||
this.cache.delete(key);
|
||||
this.cache.set(key, cached);
|
||||
}
|
||||
return cached.value;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<SummalyResult>;
|
||||
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<SummalyResult>(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<SummalyResult> {
|
||||
@bindThis
|
||||
private async fetchSummary(url: string, lang?: string): Promise<SummalyResult> {
|
||||
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<SummalyResult> {
|
||||
const proxy = meta.urlPreviewSummaryProxyUrl!;
|
||||
@bindThis
|
||||
private fetchSummaryFromProxy(url: string, lang?: string): Promise<SummalyResult> {
|
||||
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<SummalyResult>(`${proxy}?${queryStr}`, 'application/json, */*', undefined, true);
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public dispose(): void {
|
||||
this.summaryCache.dispose();
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public onApplicationShutdown(): void {
|
||||
this.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,29 @@ describe('misc:MemoryKVCache', () => {
|
||||
cache.dispose();
|
||||
});
|
||||
|
||||
test('keeps current behavior when limit is omitted', () => {
|
||||
const cache = new MemoryKVCache<number>(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<number>(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<string>(1000);
|
||||
|
||||
Reference in New Issue
Block a user