在 Node.js 中使用 HTTP Agent 实现 keep-alive

不带 Keep-Alive 的请求

我们都知道,浏览器中多个 HTTP 请求是可以通过请求头 connection: keep-alive; 来复用 TCP 连接的,但是在 Node.js 中怎么实现呢?

比如,下面是一段简单的发起请求的代码:

js 复制代码
// server.js
const http = require('http')

http
  .createServer((req, res) => {
    res.writeHead(200)
    res.end('Hello World')
  })
  .listen(3000)

// client.js
const http = require('http')

function request() {
  http
    .request(
      {
        host: 'localhost',
        port: 3000,
        pathname: '/',
      },
      (res) => {
        let buffer = []
        res.on('data', (chunk) => {
          buffer.push(chunk)
        })
        res.on('end', () => {
          console.log(buffer.toString())
        })
      }
    )
    .end()
}

request()

setTimeout(() => {
  request()
}, 1000)

当我们用 wireshard 抓包分析师,我们可以两次请求的 client 端口是不同的,并且有两个"三次握手"的过程:

使用 http.Agent 发送带有 Keep-Alive 的请求

现在,让我们使用 http.Agent 来实现 keep-alive 请求,我们只需要加少量的代码:

js 复制代码
const agent = new http.Agent({
  keepAlive: true,
})

function request() {
  http
    .request(
      {
        agent,
        host: 'localhost',
        port: 3000,
        pathname: '/',
      },
      () => {
        // ...
      }
    )
    .end()
}

但是,wireshark 中的结果其实不会发生变化!实际上,我们需要指定 agentmaxSockets 参数:

js 复制代码
const agent = new http.Agent({
  keepAlive: true,
  maxSockets: 1,
})

为什么呢?因为 maxSockets 表示每个 host 所能建立的最大 TCP 连接数。其默认值是 Infinity。如果我们不指定它的值,那每个 HTTP 请求都会建立一个 TCP 连接。

接下来,我们修改一下代码:

js 复制代码
setTimeout(() => {
  request()
}, 10000) // 1000 -> 10000

wireshark 抓包显示:

keep-alive 又不生效了!并且我们可以看到服务端 5s 左右后发送了一个 FIN 的包。难道有什么参数可以控制 keep-alive 的超时时间么?确实,也就是 keepAliveTimeout,我们把它设置为 10s:

js 复制代码
const http = require('http')

const server = http
  .createServer((req, res) => {
    res.writeHead(200)
    res.end('Hello World')
  })
  .listen(3000)

server.keepAliveTimeout = 10000

现在 keep-alive 又可以工作了。

求关注公众号"前端游"

相关推荐
hoLzwEge13 小时前
pnpm vs npm:新一代包管理器的范式革命
前端框架·node.js
麻辣凉茶14 小时前
给阿嬤一封来自云端的信(上)
前端·node.js
codingWhat2 天前
能效平台设计方案(打通gitlab和飞书)
后端·node.js·koa
见过夏天3 天前
Node.js 常用命令全攻略
node.js
前端双越老师4 天前
我从 0 开发的 AI Agent 智语项目发布了
前端·node.js·agent
kyriewen4 天前
2026 年了,还在用 Node.js?Bun 迁移实战:20 分钟搞定,附踩坑记录
前端·javascript·node.js
donecoding5 天前
3 条命令搞定闭环 Monorepo:Lerna 版本管理 + 拓扑构建 + 自定义分发
前端·前端框架·node.js
Flynt6 天前
npm v12 来了:allowScripts 默认关闭,我的项目差点跑不起来
安全·npm·node.js
叫我Paul就好7 天前
尝试 Node 搭建后端-开发框架
node.js
风止何安啊8 天前
网课倍速痛点解决:一套前端代码实现自由控速播放器
前端·javascript·node.js