优化 / 高 / 后端服务
electron/services/session-recorder.service.ts
SessionRecorder 使用 fs.appendFileSync 同步写入 trace 文件。 流式事件频率 30-50 次/秒,每次 IO 阻塞主进程 1-5ms,累计:
fs.appendFileSync
private buffer: string[] = []; private flushTimer: NodeJS.Timeout | null = null; record(event) { this.buffer.push(JSON.stringify(event)); if (!this.flushTimer) { this.flushTimer = setTimeout(() => this.flush(), 100); } } private async flush() { if (this.buffer.length === 0) return; const data = this.buffer.join('\n') + '\n'; this.buffer = []; this.flushTimer = null; await fs.promises.appendFile(this.filePath, data); }
文件: electron/services/session-recorder.service.ts
修复: appendFileSync 改为缓冲写入:事件先 push 到内存 buffer,每 100ms 定时异步 flush(fs.promises.appendFile),将 30-50 次/秒的同步 IO 降低为 10 次/秒的批量异步 IO。stopRecording 时调用 flushSync 确保最后的 session_end 事件写入文件。
appendFileSync
fs.promises.appendFile
stopRecording
flushSync
验证: tsc --noEmit 类型检查通过。
tsc --noEmit
No dependencies set.
The note is not visible to the blocked user.
问题类型
优化 / 高 / 后端服务
文件位置
electron/services/session-recorder.service.ts问题描述
SessionRecorder 使用
fs.appendFileSync同步写入 trace 文件。流式事件频率 30-50 次/秒,每次 IO 阻塞主进程 1-5ms,累计:
影响
建议修复
修复说明
文件:
electron/services/session-recorder.service.ts修复:
appendFileSync改为缓冲写入:事件先 push 到内存 buffer,每 100ms 定时异步 flush(fs.promises.appendFile),将 30-50 次/秒的同步 IO 降低为 10 次/秒的批量异步 IO。stopRecording时调用flushSync确保最后的 session_end 事件写入文件。验证:
tsc --noEmit类型检查通过。