性能优化: 移除JSON缩进、添加队列长度限制和子节点数量上限
- 移除 JSON.stringify 的缩进参数,减少 20-40% 响应体积和 Token 消耗 - 添加指令队列最大长度限制(100),超限返回 HTTP 429 - get-hierarchy 每层子节点最多返回 50 个,超限标注 childrenTruncated - 新增性能优化建议文档和分析报告
This commit is contained in:
11
src/main.js
11
src/main.js
@@ -27,7 +27,12 @@ let isProcessingCommand = false;
|
||||
* @param {Function} fn 接受 done 回调的函数,fn(done) 中操作完成后必须调用 done()
|
||||
* @returns {Promise} 操作完成后 resolve
|
||||
*/
|
||||
const MAX_QUEUE_LENGTH = 100;
|
||||
function enqueueCommand(fn) {
|
||||
if (commandQueue.length >= MAX_QUEUE_LENGTH) {
|
||||
addLog("warn", `[CommandQueue] 指令队列已满(${MAX_QUEUE_LENGTH}),拒绝新请求`);
|
||||
return Promise.reject("队列已满,请稍后重试");
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
// 兜底超时保护:防止 fn 内部未调用 done() 导致队列永久停滞
|
||||
const timeoutId = setTimeout(() => {
|
||||
@@ -1065,7 +1070,7 @@ module.exports = {
|
||||
text: err
|
||||
? `Error: ${err}`
|
||||
: typeof result === "object"
|
||||
? JSON.stringify(result, null, 2)
|
||||
? JSON.stringify(result)
|
||||
: result,
|
||||
},
|
||||
],
|
||||
@@ -1089,6 +1094,10 @@ module.exports = {
|
||||
res.end(JSON.stringify(response));
|
||||
done();
|
||||
});
|
||||
}).catch((rejectReason) => {
|
||||
// 队列已满时返回 429
|
||||
res.writeHead(429);
|
||||
res.end(JSON.stringify({ error: String(rejectReason) }));
|
||||
});
|
||||
} catch (e) {
|
||||
if (e instanceof SyntaxError) {
|
||||
|
||||
@@ -126,13 +126,18 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
|
||||
// 如果未超出深度限制,继续递归子树
|
||||
// 如果未超出深度限制,继续递归子树(每层最多返回 50 个子节点作为安全上限)
|
||||
const MAX_CHILDREN_PER_LEVEL = 50;
|
||||
if (currentDepth < depth && node.childrenCount > 0) {
|
||||
nodeData.children = [];
|
||||
for (let i = 0; i < node.childrenCount; i++) {
|
||||
const childLimit = Math.min(node.childrenCount, MAX_CHILDREN_PER_LEVEL);
|
||||
for (let i = 0; i < childLimit; i++) {
|
||||
let childData = dumpNodes(node.children[i], currentDepth + 1);
|
||||
if (childData) nodeData.children.push(childData);
|
||||
}
|
||||
if (node.childrenCount > MAX_CHILDREN_PER_LEVEL) {
|
||||
nodeData.childrenTruncated = node.childrenCount - MAX_CHILDREN_PER_LEVEL;
|
||||
}
|
||||
}
|
||||
|
||||
return nodeData;
|
||||
|
||||
Reference in New Issue
Block a user