You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

短时间大量请求致Node.js服务器崩溃问题排查求助

问题描述

Docker上运行基于Node.js(v22)、Vue3的SSR应用,平日运行正常但会随机崩溃。查看容器日志发现,出现一系列onServerPrefetch调用getProduct的日志后,出现Axios超时错误:

预取请求日志

{"log":"onServerPrefetch ran getProduct(14425)\n","stream":"stdout","time":"2024-06-19T16:46:29.289530937Z"}
{"log":"onServerPrefetch ran getProduct(14430)\n","stream":"stdout","time":"2024-06-19T16:46:29.303259316Z"}
{"log":"onServerPrefetch ran getProduct(14427)\n","stream":"stdout","time":"2024-06-19T16:46:29.315447855Z"}
{"log":"onServerPrefetch ran getProduct(14417)\n","stream":"stdout","time":"2024-06-19T16:46:29.32901105Z"}
{"log":"onServerPrefetch ran getProduct(14419)\n","stream":"stdout","time":"2024-06-19T16:46:29.346483497Z"}
{"log":"onServerPrefetch ran getProduct(14433)\n","stream":"stdout","time":"2024-06-19T16:46:29.370365608Z"}
{"log":"onServerPrefetch ran getProduct(14424)\n","stream":"stdout","time":"2024-06-19T16:46:29.382479396Z"}
{"log":"onServerPrefetch ran getProduct(14417)\n","stream":"stdout","time":"2024-06-19T16:46:29.395289652Z"}
{"log":"onServerPrefetch ran getProduct(13182)\n","stream":"stdout","time":"2024-06-19T16:46:29.477117327Z"}

错误日志

{"log":"ProductView.vue error: AxiosError: timeout of 120000ms exceeded\n","stream":"stdout","time":"2024-06-19T16:48:29.311004059Z"}

当有大量产品查看请求时,会触发崩溃,Axios无法从同一Node.js服务器的API获取数据。需解决:

  1. 是否有全局设置可限制服务器每秒请求数来验证是否为此问题?
  2. 可能的崩溃原因有哪些?
解决方案与原因分析

一、限制服务器请求数的实现方式

可以通过Express中间件快速实现全局或局部限流,验证是否为请求过载导致的问题:

1. 全局限流(使用express-rate-limit)

安装依赖:

npm install express-rate-limit

在Express入口文件中配置:

const rateLimit = require('express-rate-limit');

// 自定义限流规则:示例为每秒最多10个请求,可根据实际场景调整
const limiter = rateLimit({
  windowMs: 1000, // 时间窗口(1秒)
  max: 10, // 每个IP在窗口内的最大请求数
  message: '请求过于频繁,请稍后再试',
  standardHeaders: true,
  legacyHeaders: false,
});

// 全局应用限流中间件
app.use(limiter);

2. 针对产品API单独限流

如果只想限制getProduct相关请求,可给对应路由单独应用限流:

// 假设产品API路由为/api/product/:id
app.get('/api/product/:id', limiter, (req, res) => {
  // 产品查询逻辑
});

部署修改后的代码,模拟大量请求测试,若崩溃不再出现,即可确认是请求过载导致的问题。

二、可能的崩溃原因分析

  1. Node.js事件循环阻塞
    SSR渲染时,每个请求都会触发onServerPrefetch发起Axios请求,大量并发请求会占用事件循环线程,导致后续请求排队超时。Node.js单线程特性下,CPU密集型操作或大量异步I/O排队都会阻塞事件循环,引发超时。

  2. Axios连接池耗尽
    Node.js默认HTTP连接池的maxSockets为5,大量并发请求会占满连接池,新请求无法获取连接而超时。可调整Axios连接池配置:

    const axios = require('axios');
    const http = require('http');
    const https = require('https');
    
    const axiosInstance = axios.create({
      httpAgent: new http.Agent({ keepAlive: true, maxSockets: 20 }),
      httpsAgent: new https.Agent({ keepAlive: true, maxSockets: 20 }),
      timeout: 120000,
    });
    
  3. 数据库连接池耗尽
    getProduct若涉及数据库查询,大量并发请求会耗尽数据库连接池,导致查询超时,进而引发Axios超时。需检查数据库连接池配置,调整最大连接数匹配并发量。

  4. Docker资源限制不足
    Docker容器默认资源配额较低,高并发下CPU、内存不足会导致Node.js进程崩溃。可通过启动命令增加资源限制:

    docker run --cpus=2 --memory=2g your-image-name
    
  5. 重复请求未去重
    日志中出现重复的getProduct(14417)请求,说明存在重复渲染或未做请求缓存,相同产品的请求重复发起会加重服务器负载。可在onServerPrefetch中加入缓存逻辑,避免重复查询。

内容的提问来源于stack exchange,提问作者volume one

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 02:49:53