主要改动: 1. TCP缓冲区优化(16MB)- 解决公网上传速度慢问题 2. 并发数降低到1-3 - 避免4并发导致ERR_CONNECTION_RESET 3. 动态并发控制 - 根据网络环境自动调整 4. Nginx超时增加到120s - 支持大分片上传 5. 上传进度修复 - 修复剩余时间计算 6. 新增任务-页面上传器和详情组件 7. 统一部署文档为deploy-remote-build.ps1 问题修复: - 公网用户上传从200-900KB/s提升到预期3-5MB/s - 内网用户上传更稳定,无连接重置错误 - UploadProgress组件剩余时间计算修正 Co-Authored-By: Claude <noreply@anthropic.com>
97 lines
2.7 KiB
TypeScript
97 lines
2.7 KiB
TypeScript
/**
|
||
* 内存监控模块
|
||
*
|
||
* 定期记录内存使用情况,帮助发现内存泄漏
|
||
*/
|
||
|
||
// 定期记录内存使用(每5分钟)
|
||
const MEMORY_LOG_INTERVAL = 5 * 60 * 1000; // 5分钟
|
||
|
||
// 内存使用历史(保留最近24小时的数据)
|
||
const memoryHistory: Array<{
|
||
timestamp: number;
|
||
rss: number;
|
||
heapUsed: number;
|
||
heapTotal: number;
|
||
}> = [];
|
||
|
||
const MAX_HISTORY_SIZE = (24 * 60 * 60 * 1000) / MEMORY_LOG_INTERVAL; // 24小时
|
||
|
||
function formatBytes(bytes: number): string {
|
||
return `${(bytes / 1024 / 1024).toFixed(2)} MB`;
|
||
}
|
||
|
||
function logMemoryUsage() {
|
||
const usage = process.memoryUsage();
|
||
const uptime = process.uptime();
|
||
|
||
// 记录到历史
|
||
memoryHistory.push({
|
||
timestamp: Date.now(),
|
||
rss: usage.rss,
|
||
heapUsed: usage.heapUsed,
|
||
heapTotal: usage.heapTotal,
|
||
});
|
||
|
||
// 限制历史大小
|
||
if (memoryHistory.length > MAX_HISTORY_SIZE) {
|
||
memoryHistory.shift();
|
||
}
|
||
|
||
// 计算内存增长率(与1小时前对比)
|
||
const oneHourAgo = Date.now() - 60 * 60 * 1000;
|
||
const oldRecord = memoryHistory.find(r => r.timestamp >= oneHourAgo);
|
||
|
||
let growthInfo = '';
|
||
if (oldRecord) {
|
||
const rssGrowth = usage.rss - oldRecord.rss;
|
||
const heapGrowth = usage.heapUsed - oldRecord.heapUsed;
|
||
growthInfo = ` | 1h增长: RSS ${formatBytes(rssGrowth)}, Heap ${formatBytes(heapGrowth)}`;
|
||
}
|
||
|
||
console.log(
|
||
`[Memory Monitor] RSS: ${formatBytes(usage.rss)}, ` +
|
||
`Heap: ${formatBytes(usage.heapUsed)}/${formatBytes(usage.heapTotal)}, ` +
|
||
`External: ${formatBytes(usage.external)}, ` +
|
||
`Uptime: ${(uptime / 3600).toFixed(2)}h${growthInfo}`
|
||
);
|
||
|
||
// 如果内存使用超过阈值,发出警告
|
||
const RSS_WARNING_THRESHOLD = 2 * 1024 * 1024 * 1024; // 2GB
|
||
const HEAP_WARNING_THRESHOLD = 1.5 * 1024 * 1024 * 1024; // 1.5GB
|
||
|
||
if (usage.rss > RSS_WARNING_THRESHOLD) {
|
||
console.warn(`[Memory Monitor] ⚠️ RSS内存使用过高: ${formatBytes(usage.rss)} (阈值: 2GB)`);
|
||
}
|
||
|
||
if (usage.heapUsed > HEAP_WARNING_THRESHOLD) {
|
||
console.warn(`[Memory Monitor] ⚠️ Heap内存使用过高: ${formatBytes(usage.heapUsed)} (阈值: 1.5GB)`);
|
||
}
|
||
}
|
||
|
||
// 启动内存监控
|
||
export function startMemoryMonitor() {
|
||
console.log('[Memory Monitor] 启动内存监控,间隔: 5分钟');
|
||
|
||
// 立即记录一次
|
||
logMemoryUsage();
|
||
|
||
// 定期记录
|
||
setInterval(logMemoryUsage, MEMORY_LOG_INTERVAL);
|
||
}
|
||
|
||
// 获取内存历史数据
|
||
export function getMemoryHistory() {
|
||
return memoryHistory.map(record => ({
|
||
timestamp: new Date(record.timestamp).toISOString(),
|
||
rss: formatBytes(record.rss),
|
||
heapUsed: formatBytes(record.heapUsed),
|
||
heapTotal: formatBytes(record.heapTotal),
|
||
}));
|
||
}
|
||
|
||
// 自动启动(在模块加载时)
|
||
if (process.env.NODE_ENV === 'production') {
|
||
startMemoryMonitor();
|
||
}
|