feat(ts): 内存压力触发的缓存 LRU 卸载,与 rust 端策略对齐

- subset_queue 暴露 memoryUnderPressure(cgroup 可用 <15% 判定,复用 readMemorySnapshot)
- LruCache 增加 evictOldest 原语
- app 启动 60s 压力卸载循环:先清裁剪产物缓存再清字体 buffer 缓存,水位回升即停
This commit is contained in:
崮生(子虚) 2026-11-04 20:18:43 +08:00
parent 9edeaa89b7
commit 24a32f72ba
3 changed files with 45 additions and 2 deletions

View File

@ -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();
}

View File

@ -60,6 +60,18 @@ export class LruCache<V> {
}
}
/** 淘汰最久未使用的一个条目;缓存已空返回 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!) {

View File

@ -56,6 +56,15 @@ async function readMemorySnapshot(): Promise<MemorySnapshot> {
return { limit, free, reserve };
}
/**
* 容器内存压力判定:可用(cgroup 优先)低于限额的 15%。
* 供缓存压力卸载循环使用,与 withMemoryGate 的准入判断相互独立。
*/
export async function memoryUnderPressure(): Promise<boolean> {
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;