perf: 性能与可靠性优化 - CommandQueue超时恢复/HTTP限制/日志轮转/调试日志清理/applyProperties修复
This commit is contained in:
36
main.js
36
main.js
@@ -29,7 +29,14 @@ let isProcessingCommand = false;
|
||||
*/
|
||||
function enqueueCommand(fn) {
|
||||
return new Promise((resolve) => {
|
||||
commandQueue.push({ fn, resolve });
|
||||
// 兜底超时保护:防止 fn 内部未调用 done() 导致队列永久停滞
|
||||
const timeoutId = setTimeout(() => {
|
||||
addLog("error", "[CommandQueue] 指令执行超时(60s),强制释放队列");
|
||||
isProcessingCommand = false;
|
||||
resolve();
|
||||
processNextCommand();
|
||||
}, 60000);
|
||||
commandQueue.push({ fn, resolve, timeoutId });
|
||||
processNextCommand();
|
||||
});
|
||||
}
|
||||
@@ -40,15 +47,17 @@ function enqueueCommand(fn) {
|
||||
function processNextCommand() {
|
||||
if (isProcessingCommand || commandQueue.length === 0) return;
|
||||
isProcessingCommand = true;
|
||||
const { fn, resolve } = commandQueue.shift();
|
||||
const { fn, resolve, timeoutId } = commandQueue.shift();
|
||||
try {
|
||||
fn(() => {
|
||||
clearTimeout(timeoutId);
|
||||
isProcessingCommand = false;
|
||||
resolve();
|
||||
processNextCommand();
|
||||
});
|
||||
} catch (e) {
|
||||
// 防止队列因未捕获异常永久阻塞
|
||||
clearTimeout(timeoutId);
|
||||
addLog("error", `[CommandQueue] 指令执行异常: ${e.message}`);
|
||||
isProcessingCommand = false;
|
||||
resolve();
|
||||
@@ -112,6 +121,19 @@ function getLogFilePath() {
|
||||
fs.mkdirSync(settingsDir, { recursive: true });
|
||||
}
|
||||
_logFilePath = pathModule.join(settingsDir, "mcp-bridge.log");
|
||||
// 日志轮转: 超过 2MB 时备份旧日志并创建新文件
|
||||
try {
|
||||
if (fs.existsSync(_logFilePath)) {
|
||||
const stats = fs.statSync(_logFilePath);
|
||||
if (stats.size > 2 * 1024 * 1024) {
|
||||
const backupPath = _logFilePath + ".old";
|
||||
if (fs.existsSync(backupPath)) fs.unlinkSync(backupPath);
|
||||
fs.renameSync(_logFilePath, backupPath);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
/* 轮转失败不影响主流程 */
|
||||
}
|
||||
return _logFilePath;
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -832,11 +854,21 @@ module.exports = {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
|
||||
const MAX_BODY_SIZE = 5 * 1024 * 1024; // 5MB 请求体上限
|
||||
let body = "";
|
||||
let aborted = false;
|
||||
req.on("data", (chunk) => {
|
||||
body += chunk;
|
||||
if (body.length > MAX_BODY_SIZE) {
|
||||
aborted = true;
|
||||
addLog("error", `[HTTP] 请求体超过 ${MAX_BODY_SIZE} 字节上限,已拒绝`);
|
||||
res.writeHead(413);
|
||||
res.end(JSON.stringify({ error: "请求体过大" }));
|
||||
req.destroy();
|
||||
}
|
||||
});
|
||||
req.on("end", () => {
|
||||
if (aborted) return;
|
||||
const url = req.url;
|
||||
if (url === "/list-tools") {
|
||||
const tools = getToolsList();
|
||||
|
||||
Reference in New Issue
Block a user