V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
MuscleOf2016
0D
V2EX  ›  Node.js

使用 needle 发起请求,经常 socket hang up 报错

  •  
  •   MuscleOf2016 · Sep 14, 2022 · 3673 views
    This topic created in 1321 days ago, the information mentioned may be changed or developed.

    这是为啥,

    1 replies    2022-09-15 08:14:59 +08:00
    iPc666
        1
    iPc666  
       Sep 15, 2022
    问: 为什么使用 http 模块发起 HTTP 客户端请求时,有时候会发生 socket hang up 错误?

    答: 发起客户端 HTTP 请求前需要先创建一个客户端。http 模块提供了一个全局客户端 http.globalAgent ,可以让我们使用.request 或.get 方法时不用手动创建客户端。但是全局客户端默认只允许 5 个并发 Socket 连接,当某一个时刻 HTTP 客户端请求创建过多,超过这个数字时,就会发生 socket hang up 错误。解决方法也很简单,通过 http.globalAgent.maxSockets 属性把这个数字改大些即可。另外,https 模块遇到这个问题时也一样通过 https.globalAgent.maxSockets 属性来处理。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6112 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 61ms · UTC 02:10 · PVG 10:10 · LAX 19:10 · JFK 22:10
    ♥ Do have faith in what you're doing.