web-font/backend/config.ts
崮生(子虚) 1ffb1632b2 glyph-match 重构: 锚点索引+聚类剪枝+串行事件收集, 修复响应双 header 截断
- 慢路径 subset(查询字+锚点字) 顺带建 .gidx, 冷启动 39.75s→0.43s
- 去 async generator/worker 池/流式 pipeTo: LLRT 下非确定性饿死/segfault
- eventsCache(fontPath+查询签名 LRU64): 重复查询免裁剪, 稳态 42ms
- server 层 _bodyBytes 通道: header+body 单帧写回 + Content-Length
- 修复 bodyBytes 分支头部双写导致响应尾部截断(done 事件丢失)
- tcp 层 fire-and-forget write, close 只 end 不 destroy(保写缓冲)
- 慢路径并发度 GLYPH_MATCH_SLOW_CONCURRENCY 可配(默认1)
- 聚类排序+剪枝(PRUNE_MARGIN=0.35)减少冷路径裁剪量
2026-11-02 20:26:58 +08:00

48 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 从环境变量读取服务配置,启动时一次性加载
*/
const env = globalThis.process?.env ?? {};
/** 临时上传开关 */
export const enableTempUpload = env.ENABLE_TEMP_UPLOAD === "true";
/** 管理员 API Key,为空则管理员上传不可用 */
export const adminApiKey: string = env.ADMIN_API_KEY ?? "";
/** 临时上传目录最大文件数 */
export const tempMaxFiles = parseInt(env.TEMP_MAX_FILES ?? "10", 10) || 10;
/** 临时上传目录总体积上限(字节),默认 200MB */
export const tempMaxTotalSize = parseInt(env.TEMP_MAX_TOTAL_SIZE ?? `${200 * 1024 * 1024}`, 10) || 200 * 1024 * 1024;
/** 临时字体保留时限(秒),超过后若无人使用则自动删除 */
export const tempRetentionSeconds = parseInt(env.TEMP_RETENTION_SECONDS ?? "10800", 10) || 10800;
/** 字体裁剪结果内存缓存容量上限(字节),默认 10MB */
export const subsetCacheMaxSize = parseInt(env.SUBSET_CACHE_MAX_SIZE ?? `${10 * 1024 * 1024}`, 10) || 10 * 1024 * 1024;
/**
* 字体子集化最大并发数
*
* 字体裁剪是 CPU/内存密集操作,并发过多会导致 LLRT OOM 崩溃。
* 默认 4:在 900M 内存限制下安全运行。
* 内存充裕可调大,内存紧张可调小到 2。
*/
export const subsetConcurrency = Math.max(1, parseInt(env.SUBSET_CONCURRENCY ?? "4", 10) || 4);
/**
* 队列等待超时(秒)—— 排队超过此时间返回 503,避免请求无限堆积
*/
export const subsetQueueTimeoutSeconds = Math.max(5, parseInt(env.SUBSET_QUEUE_TIMEOUT ?? "30", 10) || 30);
/**
* 字形搜索慢路径(源字体裁剪解析)最大并发数
*
* 单字体解析后的对象图可达上百 MB,是搜索链路里最重的操作。
* 默认 1:LLRT 运行时 GC 回收滞后 + 串行最稳;Node 或内存充裕的环境可调大。
*/
export const glyphMatchSlowConcurrency = Math.max(1, parseInt(env.GLYPH_MATCH_SLOW_CONCURRENCY ?? "1", 10) || 1);
/** 字体搜索目录(按优先级排序:admin > 普通 > 临时) */
export const fontDirs = ["font/admin", "font", "font/temp"] as const;