DEBUG.WIKI
阿里云;从建站到云上业务;云服务器 · 数据库 · 对象存储;查看产品 →
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
IPPeak:从目标地区访问网页,住宅代理、SEO 监测、广告验证

解决 Locust 报错 CatchResponseError: Response timed out 超时抖动修复

排查 Locust 压测过程中突发大量 CatchResponseError: Request timed out 并伴随 RPS 断崖下跌,分析 后端连接池耗尽与网络拥塞。

问题现象

加压到 1000 用户时,Locust 失败率从 0% 暴增至 60%,统计报表显示:

CatchResponseError: HTTPConnectionPool(host='api.example.com', port=443): Read timed out. (read timeout=5.0)

触发根因

  1. 后端服务在突发高并发下数据库连接池被耗尽,请求在应用内部队列严重积压超出客户端设置的 5 秒超时限制;
  2. Locust 客户端客户端连接池限制(pool_maxsize)过小导致发包线程自身等待分配 Socket。

解决步骤

在 Locust 用户类中扩大底层的连接池大小

自定义 HTTPClient 属性:

from locust import HttpUser, task

class HeavyUser(HttpUser):
    def on_start(self):
        # 扩大连接池
        self.client.mount('https://', requests.adapters.HTTPAdapter(
            pool_connections=100,
            pool_maxsize=1000
        ))

为高耗时接口适当调大超时阈值

配置灵活超时:

self.client.get("/api/v1/export", timeout=30)

验证确认

重新执行阶梯加压测试:

RPS 突破新高,超时断言失败率清零,精准摸清系统吞吐瓶颈。