如何使用curl\u easy重用连接?

如何使用curl\u easy重用连接?,curl,tcp,julia,libcurl,Curl,Tcp,Julia,Libcurl,我正在用libcurl的curl\u easy接口编写代码,这让我很为难 让它重用连接。详情如下 curl --version curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.1 zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3 Release-Date: 2018-01-24 Protocols: dic

我正在用libcurl的
curl\u easy
接口编写代码,这让我很为难 让它重用连接。详情如下

curl --version
curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.1 zlib/1.2.11
libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3
Release-Date: 2018-01-24
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3
pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB
SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL
我的代码运行一系列单元测试,所有这些测试都必须连接到 H2上的主机相同,因此我设置了持久连接。我打电话 curl\u easy\u init一次,然后是所有全局
curl\u easy\u setopt
值,然后在 循环,我通过调用
curl\u easy\u setopt
设置post字段来发出请求, url、内容长度,然后
curl\u easy\u perform
。 我只在发出所有请求后调用
curl\u easy\u cleanup
。 我设置了以下选项:

CURLOPT_FOLLOWLOCATION => 1
CURLOPT_SSL_VERIFYPEER => 1
CURLOPT_SSL_VERIFYHOST => 2
CURLOPT_SSLVERSION 7<<16
CURLOPT_HTTP_VERSION = 4
CURLOPT_TCP_FASTOPEN = 1
CURLOPT_TCP_KEEPALIVE = 1
CURLOPT_ACCEPT_ENCODING = ""
CURLOPT_TRANSFER_ENCODING = 1
CURLOPT_POST = 1
CURLOPT_DNS_CACHE_TIMEOUT = 0
CURLOPT_VERBOSE = 1
CURLOPT_SSLCERT
CURLOPT_SSLKEY
CURLOPT_CAINFO
等等

它很快就会变成这样:

* Connection cache is full, closing the oldest one.
* Closing connection 0
* Connection #5 to host XYZ left intact
最旧连接的关闭将从此处继续,直到 要么一切都完成了,要么TLS握手错误导致连接中断 进入不一致的状态

你知道我能做些什么让curl重用到主机的H2连接吗


谢谢您的时间。

问题在于
CURLOPT\u TCP\u FASTOPEN
。关闭该选项将允许重新使用连接。只有在使用多个TCP连接以避免握手过程中的往返时,才需要FAST_OPEN

传统的TCP握手是一种三路协议

  • 发起方向对等方发送SYN数据包
  • 对等方向发起方发送SYN-ACK数据包进行响应
  • 最后,发起方发送自己的ACK
在发送ACK之后,发起方发送第一个数据包。总共有4个数据包发送数据

打开FAST_后,数据传输(在第一次连接后的连接上)可以从第一个数据包开始。它使用TCP Cookie(TFO)来确定这两台主机最近已成功地相互连接


有关更多信息,请参阅。

不完全是一个问题,但我确实似乎是一个难题。如果您为正在使用的编程语言添加一个标记,这可能会有所帮助,它肯定会增加Q的读者数量。祝您好运。@Sheller谢谢,我最终修改了代码,找到了问题所在。它使用的是
TCP\u FASTOPEN
。如果我把它去掉,连接会被重复使用。你能链接/提供更多关于什么是“握手时的往返”及其带来的负面影响的详细信息吗?谢谢。我在评论@T.Todua中添加了更多细节
* Found bundle for host XYZ: 0xe141520 [can multiplex]
* Connection #0 is still name resolving, can't reuse
* Trying IP...
* TCP_NODELAY set
* TCP_FASTOPEN_CONNECT set
* Connected to XYZ () port 443 (#1)
* found 3 certificates in /data/certs/ca_chain.pem
* found 401 certificates in /etc/ssl/certs
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL re-using session ID
* SSL connection using TLS1.2 / ECDHE_RSA_AES_128_GCM_SHA256
* server certificate verification OK
* server certificate status verification SKIPPED
* error fetching CN from cert:The requested data were not available.
* common name: (matched)
* server certificate expiration date OK
* server certificate activation date OK
* certificate public key: RSA
* certificate version: #3
* subject:
* start date: Mon, 13 Jul 2020 08:04:30 GMT
* expire date: Mon, 20 Jul 2020 08:05:00 GMT
* issuer: C=US,ST=MA,L=Cambridge,O=XXXXX,CN=YYYYY
* compression: NULL
* ALPN, server accepted to use h2
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade:
len=0
* Using Stream ID: 1 (easy handle 0x9036570)

<Request>

* We are completely uploaded and fine
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
< HTTP/2 200
< vary: accept-encoding
< content-encoding: gzip
< content-type: text/plain
< date: Mon, 13 Jul 2020 19:30:36 GMT
< server: abcdefg

<Response>

* Connection #1 to host XYZ left intact
* Found bundle for host XYZ: 0xe141520 [can multiplex]
* Connection #0 is still name resolving, can't reuse
* Connection #1 is still name resolving, can't reuse
* Connection cache is full, closing the oldest one.
* Closing connection 0
* Connection #5 to host XYZ left intact