卷曲未正确超时

Curl not timing out properly

本文关键字:超时      更新时间:2023-10-16

我设置了CURLOPT_CONNECTTIMEOUT_MS = 200和CURLOPT_TIMEOUT_MS = 70 ms。但我看到CURLINFO_TOTAL_TIME在 220 毫秒左右。

根据 libcurl 文档,CURLOPT_TIMEOUT_MS还包括连接超时。所以基本上我的 curl 调用总时间不应该超过 70 毫秒。但是,为什么要收回更多的控制权呢?

有人可以解释一下这种行为吗?

我正在使用 curl 7.19_02 C++库。

这是我的代码

CURL * curl;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT_MS,200);
curl_easy_setopt(curl,CURLOPT_TIMEOUT_MS,70);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 
double tt = 0.0;
double ns = 0.0;
double ct = 0.0;
double pt = 0.0;
double st = 0.0;
curl_easy_perform(curl);
int curlRC = curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &tt);
curlRC = curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME, &ns);
curlRC = curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &ct);
curlRC = curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &pt);
curlRC = curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME, &st);
cout << "Curl timing info: Total: " << tt << endl << " Lookup: "<< ns << endl << "    Connect: " << ct << "n" << "pre transfer: " << pt << endl << "start transfer: " << st <<endl;

我得到的时间信息如下。请检查一下

卷曲计时信息: 总计:0.216793

查找: 0.000999

连接: 0.023199

预转移:0.023213

开始传输:0.216667

所以关键是,预转移和开始转移之间发生了什么?

这是 libcurl 到 7.20.0 版本的错误。在 7.20.1 中,输出与预期一致:

Curl timing info: Total: 0.071098
Lookup: 0.000116
Connect: 0.000303
pre transfer: 0.000327
start transfer: 0

我找不到修复该错误的变更集。但可能是在 cURL 更改中的"亚秒级超时改进"<</p>

div class="answers>

所以关键是,预转移和开始转移之间发生了什么?

这是服务器计算结果(处理请求)并准备好发送响应的第一个字节所花费的实际时间。简而言之,这是请求的实际服务器时间,您会期望这是最大的部分