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

2023-04-25 13:05:09 +08:00
 5bb864e1fc775087

请求 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
1070 次点击
所在节点    问与答
10 条回复
0o0O0o0O0o
2023-04-25 13:09:05 +08:00
跟多个 url 就可以了吧
5bb864e1fc775087
2023-04-25 13:34:42 +08:00
@0o0O0o0O0o 我没说清楚, 我是想实现第二次执行命令去请求 bing.com 的时候能复用第一次的 TCP 链接
leozzf
2023-04-25 13:43:34 +08:00
那你得把 curl 作为一个 daemon 后台进程使用
crystom
2023-04-25 13:49:31 +08:00
这跟 http2 没关系,只需要 http1.1 就能复用,但是命令行中确实几次 curl 之间没法复用
lambdaq
2023-04-25 13:53:34 +08:00
@5bb864e1fc775087 原理上来说几乎不可能了。一个连接必须对应一个进程。进程没了,fd 就被销毁了。第二个进程没法接着用。除非你用了什么 trick
0o0O0o0O0o
2023-04-25 13:56:03 +08:00
@5bb864e1fc775087 我就是这个意思,一条命令里接多个 bing.com 的 url ,第一个 url 以后的请求就可以复用 connection 了吧,verbose 应该能观察到。
hahastudio
2023-04-25 14:00:01 +08:00
https://curl.se/docs/manpage.html 里面提到的 --next ?然后我猜应该不需要用 --parallel-immediate ?
你给的文档我猜是调用 libcurl 时候才去看的
tool2d
2023-04-25 14:11:50 +08:00
@crystom "这跟 http2 没关系,只需要 http1.1 就能复用"

还有一点区别的,http1.1 只是复用 socket 连接。而 http2 会复用压缩字典,让后面头部压缩数据传输量,异常的小。
0o0O0o0O0o
2023-04-25 16:27:18 +08:00
只是复用连接,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
2023-04-25 16:42:18 +08:00
复用 TCP 连接本来就支持
https://everything.curl.dev/usingcurl/persist

复用头部动态压缩表还有多路复用,是不行的,进程都退出了,内存都归还了,除非做成守护进程

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/935326

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX