V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
5bb864e1fc775087
V2EX  ›  问与答

在命令行中, 有办法让 CURL 把 HTTP2 Multiplexing 特性用上么

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

    请求 https://bing.com 在 POSTMAN 里耗时 60ms 左右 , 而在 curl 命令行里却要 200ms
    我怀疑是 TCP 链接没有复用, 每次执行 curl 命令都要重建 TCP 链接导致的, 所以想试着开启 HTTP2 Multiplexing, 官网倒是有介绍, https://curl.se/docs/http2.html 但不知道怎么开, 以及能不能在命令行中开启

    以下是 CURL 命令和所输出的结果

    curl  \
    -w "time_namelookup:  %{time_namelookup}\n time_connect:  %{time_connect}\n time_appconnect:  %{time_appconnect}\n time_redirect:  %{time_redirect}\n time_pretransfer:  %{time_pretransfer}\n time_starttransfer:  %{time_starttransfer}\n time_total:  %{time_total}\n" \
    -o /dev/null -s \
    --http2 \
    'https://bing.com'
    
    time_namelookup:  0.010229
    time_connect:  0.069974
    time_appconnect:  0.195230
    time_redirect:  0.000000
    time_pretransfer:  0.195348
    time_starttransfer:  0.257910
    time_total:  0.257964
    
    10 条回复    2023-04-25 16:42:18 +08:00
    0o0O0o0O0o
        1
    0o0O0o0O0o  
       338 天前 via iPhone
    跟多个 url 就可以了吧
    5bb864e1fc775087
        2
    5bb864e1fc775087  
    OP
       338 天前
    @0o0O0o0O0o 我没说清楚, 我是想实现第二次执行命令去请求 bing.com 的时候能复用第一次的 TCP 链接
    leozzf
        3
    leozzf  
       338 天前 via Android
    那你得把 curl 作为一个 daemon 后台进程使用
    crystom
        4
    crystom  
       338 天前
    这跟 http2 没关系,只需要 http1.1 就能复用,但是命令行中确实几次 curl 之间没法复用
    lambdaq
        5
    lambdaq  
       338 天前
    @5bb864e1fc775087 原理上来说几乎不可能了。一个连接必须对应一个进程。进程没了,fd 就被销毁了。第二个进程没法接着用。除非你用了什么 trick
    0o0O0o0O0o
        6
    0o0O0o0O0o  
       338 天前 via iPhone
    @5bb864e1fc775087 我就是这个意思,一条命令里接多个 bing.com 的 url ,第一个 url 以后的请求就可以复用 connection 了吧,verbose 应该能观察到。
    hahastudio
        7
    hahastudio  
       338 天前
    https://curl.se/docs/manpage.html 里面提到的 --next ?然后我猜应该不需要用 --parallel-immediate ?
    你给的文档我猜是调用 libcurl 时候才去看的
    tool2d
        8
    tool2d  
       338 天前
    @crystom "这跟 http2 没关系,只需要 http1.1 就能复用"

    还有一点区别的,http1.1 只是复用 socket 连接。而 http2 会复用压缩字典,让后面头部压缩数据传输量,异常的小。
    0o0O0o0O0o
        9
    0o0O0o0O0o  
       338 天前
    只是复用连接,http1.1 就可以

    curl -v --http1.1 -o /dev/null -sI https://www.bing.com https://www.bing.com https://www.bing.com 2>&1 | grep -w 'multiplex\|connection'

    看 multiplexing 支持

    curl -v --http2 -o /dev/null -sI https://www.bing.com https://www.bing.com https://www.bing.com 2>&1 | grep -w 'multiplex\|connection'
    lhbc
        10
    lhbc  
       338 天前 via Android
    复用 TCP 连接本来就支持
    https://everything.curl.dev/usingcurl/persist

    复用头部动态压缩表还有多路复用,是不行的,进程都退出了,内存都归还了,除非做成守护进程
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   978 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:56 · PVG 05:56 · LAX 14:56 · JFK 17:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.