V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iqoo
V2EX  ›  程序员

NodeJS 中 HTTP2 的一个诡异问题

  •  
  •   iqoo · 337 天前 · 1290 次点击
    这是一个创建于 337 天前的主题,其中的信息可能已经有所发展或是发生改变。

    客户端和服务端都是 HTTP2 。

    用 curl 从服务端下载数据可跑满服务器带宽(约 4MB/s ),用 NodeJS 的客户端下载只能跑到 1MB/s ,说明服务端没问题,可能是客户端的问题。

    但奇怪的是,把客户端的测试 URL 换成网上某个 CDN 大文件时,也能达到很高的下载速度,这样看客户端也没问题。

    这里附上精简后的代码。服务端:

    import fs from 'fs'
    import http2 from 'http2'
    
    const h2Server = http2.createSecureServer({
      key: fs.readFileSync('ecc.key'),
      cert: fs.readFileSync('ecc.cer')
    })
    
    h2Server.on('stream', (stream) => {
      stream.on('error', () => {})
      stream.respond()
    
      const buf = Buffer.alloc(65536)
      const next = () => {
        stream.write(buf, err => {
          if (err) {
            stream.end()
          } else {
            next()
          }
        })
      }
      next()
    }).listen(443)
    

    客户端:

    import http2 from 'http2'
    
    const url = new URL('https://........')
    
    const h2session = http2.connect(url, () => {
      const stream = h2session.request({
        ':path': url.pathname,
        ':authority': url.host,
      })
      stream.on('response', () => {
        let bps = 0
        stream.on('data', chunk => {
          bps += chunk.length
        })
        setInterval(() => {
          console.log('speed:', bps)
          bps = 0
        }, 1000)
      })
    })
    

    在本地跑看不出问题,毕竟本地通信带宽极大,放到服务器上就有问题了。不知是踩到哪个坑了。。。

    8 条回复    2023-05-27 16:54:56 +08:00
    july1995
        1
    july1995  
       337 天前 via Android
    等一个大佬答复, 长长见识。
    tool2d
        2
    tool2d  
       337 天前
    本地没问题,那肯定就是服务器机房触发了某种限速规则。

    可以测试一下 http1 速度,我个人感觉和代码关系不大,http 是行业标准,nodejs 库不太可能出大问题。
    iqoo
        3
    iqoo  
    OP
       337 天前
    @tool2d http1 没问题。和机房关系不大,测试了多个机房,都存在这个现象。

    只要客户端和服务端都是 nodejs 的 http2 ,它们之间可能就会降速。尝试让双方都增加窗口大小也没用。
    zbinlin
        4
    zbinlin  
       337 天前
    抓个包来看下呀
    luojiyin87
        5
    luojiyin87  
       337 天前
    捉包看网络收发窗口信息
    rrfeng
        6
    rrfeng  
       337 天前 via Android
    只能抓包看了
    http2 有时候确实会带宽跑不起来。
    wzy44944
        8
    wzy44944  
       336 天前
    这种问题最好抓包看下窗口大小,要是接收端窗口太小的问题,可能是接收缓存设置的太小
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   946 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:39 · PVG 06:39 · LAX 15:39 · JFK 18:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.