From 24a32f72ba8fd9d5228079abd4e9a58b5fa0bd43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=AE=E7=94=9F=EF=BC=88=E5=AD=90=E8=99=9A=EF=BC=89?= <2234839456@qq.com> Date: Wed, 4 Nov 2026 20:18:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(ts):=20=E5=86=85=E5=AD=98=E5=8E=8B?= =?UTF-8?q?=E5=8A=9B=E8=A7=A6=E5=8F=91=E7=9A=84=E7=BC=93=E5=AD=98=20LRU=20?= =?UTF-8?q?=E5=8D=B8=E8=BD=BD=EF=BC=8C=E4=B8=8E=20rust=20=E7=AB=AF?= =?UTF-8?q?=E7=AD=96=E7=95=A5=E5=AF=B9=E9=BD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - subset_queue 暴露 memoryUnderPressure(cgroup 可用 <15% 判定,复用 readMemorySnapshot) - LruCache 增加 evictOldest 原语 - app 启动 60s 压力卸载循环:先清裁剪产物缓存再清字体 buffer 缓存,水位回升即停 --- backend/app.ts | 26 ++++++++++++++++++++++++-- backend/lru_cache.ts | 12 ++++++++++++ backend/subset_queue.ts | 9 +++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/backend/app.ts b/backend/app.ts index 5229f9f..f34188a 100644 --- a/backend/app.ts +++ b/backend/app.ts @@ -13,7 +13,7 @@ import { mimeTypes } from "./server/mime_type"; import type { cMiddleware } from "./server/req_res"; import { SimpleHttpServer } from "./server/server"; import { path_join, readFile, stat, mkdir } from "./interface"; -import { parseUrl, jsonResponse, stats, initStats, markStatsDirty } from "./shared"; +import { parseUrl, jsonResponse, stats, initStats, markStatsDirty, subsetCache, fontBufferCache } from "./shared"; import { flushStatsSyncSafe } from "./stats_store"; import { enableTempUpload, adminApiKey } from "./config"; import { handleListFonts } from "./routes/fonts"; @@ -25,7 +25,7 @@ import { handleGlyphMatch } from "./routes/glyph_match"; import { handleFontDetail } from "./routes/font_detail"; import { handleFontMeta } from "./routes/font_meta"; import { startTempCleaner } from "./temp_cleaner"; -import { initMemoryGate } from "./subset_queue"; +import { initMemoryGate, memoryUnderPressure } from "./subset_queue"; import { subsetConcurrency } from "./config"; const ROOT_DIR = "dist"; @@ -231,6 +231,28 @@ async function main() { /** 初始化子集化并发队列(含字体分组调度) */ initMemoryGate(subsetConcurrency); + /** + * 缓存压力卸载兜底:每 60s 检查内存水位,可用 <15% 时按 LRU 逐个清缓存 + * (裁剪产物缓存占大头先清,再清字体 buffer 缓存),水位回升即停。 + * 与 LRU 容量淘汰互补:容量管日常,压力管兜底。NODE_CACHE_PRESSURE_UNLOAD=0 可关。 + */ + if (process.env.NODE_CACHE_PRESSURE_UNLOAD !== "0") { + setInterval(async () => { + try { + if (!(await memoryUnderPressure())) return; + let n = 0; + while (await memoryUnderPressure()) { + if (subsetCache.evictOldest()) n++; + else if (fontBufferCache.evictOldest()) n++; + else break; + } + if (n > 0) console.log(`[cache-pressure-unload] 内存压力,LRU 卸载 ${n} 个缓存条目`); + } catch (e) { + console.error("[cache-pressure-unload] 检查失败:", e); + } + }, 60_000).unref(); + } + /** 启动临时字体定时清理器 */ startTempCleaner(); } diff --git a/backend/lru_cache.ts b/backend/lru_cache.ts index cb92da3..e3368f9 100644 --- a/backend/lru_cache.ts +++ b/backend/lru_cache.ts @@ -60,6 +60,18 @@ export class LruCache { } } + /** 淘汰最久未使用的一个条目;缓存已空返回 false 表示无可再卸 */ + evictOldest(): boolean { + const oldest = this.cache.keys().next().value; + if (oldest === undefined) return false; + const entry = this.cache.get(oldest)!; + this.cache.delete(oldest); + if (this.sizeFn) { + this.usedBytes -= this.sizeFn(entry); + } + return true; + } + /** 按条目数淘汰 */ private evictByCount() { while (this.cache.size > this.maxSize!) { diff --git a/backend/subset_queue.ts b/backend/subset_queue.ts index d138e7b..3d2b6e6 100644 --- a/backend/subset_queue.ts +++ b/backend/subset_queue.ts @@ -56,6 +56,15 @@ async function readMemorySnapshot(): Promise { return { limit, free, reserve }; } +/** + * 容器内存压力判定:可用(cgroup 优先)低于限额的 15%。 + * 供缓存压力卸载循环使用,与 withMemoryGate 的准入判断相互独立。 + */ +export async function memoryUnderPressure(): Promise { + const { limit, free } = await readMemorySnapshot(); + return free < limit * 0.15; +} + /** 小内存机器最多同时执行两个重任务;大内存机器仍受配置上限约束 */ function concurrencyLimit(memory: MemorySnapshot): number { return memory.limit <= 2 * 1024 * MIB ? Math.min(maxConcurrency, 2) : maxConcurrency;