cURL:空闲超时间隔超过指定值
我正在使用 libcurl 创建到服务器的 http 连接。在初始化期间,我指定了 5 秒的空闲超时值,并指定为进度回调函数。我原本期望 cURL 在 5 秒不活动后中止连接并停止调用进度回调,但我发现 cURL 在大约 15 秒后超时。为什么curl 超时的时间比我指定的要长?将超时设置为较大的值并没有帮助。如果我指定 100 秒,则在 105 秒不活动后超时。
code = s_curl_easy_setopt(m_curl_handle, CURLOPT_NOPROGRESS, 0);
assert(code == CURLE_OK);
code = s_curl_easy_setopt(m_curl_handle, CURLOPT_PROGRESSFUNCTION, progress_callback);
assert(code == CURLE_OK);
编辑:超时代码
//this will set the timeout for quitting in case the network goes down
code = s_curl_easy_setopt(m_curl_handle, CURLOPT_LOW_SPEED_LIMIT, 1);
code = s_curl_easy_setopt(m_curl_handle, CURLOPT_LOW_SPEED_TIME, m_idle_timeout);
I am using libcurl
to create an http connection to a server. During initialization I have specified an idle timeout value of 5 seconds and also specified as progress callback function. I was expecting cURL to abort the connection after 5 seconds of inactivity and to stop calling the progress callback but I found that curl times out after around 15 seconds. Why is curl taking more time to timeout than I what I have specified? Setting timeout to a larger value does not help. If I specify 100 secs, it timesout after 105 secs of inactivity.
code = s_curl_easy_setopt(m_curl_handle, CURLOPT_NOPROGRESS, 0);
assert(code == CURLE_OK);
code = s_curl_easy_setopt(m_curl_handle, CURLOPT_PROGRESSFUNCTION, progress_callback);
assert(code == CURLE_OK);
EDIT: The timeout code
//this will set the timeout for quitting in case the network goes down
code = s_curl_easy_setopt(m_curl_handle, CURLOPT_LOW_SPEED_LIMIT, 1);
code = s_curl_easy_setopt(m_curl_handle, CURLOPT_LOW_SPEED_TIME, m_idle_timeout);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经弄清楚了这一点。 cURL 大约每秒更新一次其进度。为了计算空闲超时,cURL 计算 6 次更新的平均字节/秒,并将其与
CURLOPT_LOW_SPEED_LIMIT
进行比较。如果此值小于CURLOPT_LOW_SPEED_LIMIT
的时间超过CURLOPT_LOW_SPEED_TIME
秒,则会超时。因此,如果CURLOPT_LOW_SPEED_TIME
为 5 秒,cURL 将计算最近 6 次进度更新(大约 5 秒)的平均字节/秒,然后检查它是否小于CURLOPT_LOW_SPEED_LIMIT
至少 5 秒,因此总共需要约 20 分钟。 10秒。I have figured this one out. cURL updates its progress approximately one per second. To calculate idle timeout cURL calculates the average bytes/sec over 6 updates and compares it with the
CURLOPT_LOW_SPEED_LIMIT
. If this value is less thanCURLOPT_LOW_SPEED_LIMIT
for more thanCURLOPT_LOW_SPEED_TIME
seconds at a stretch, it times out. So if theCURLOPT_LOW_SPEED_TIME
is 5 seconds, cURL will calculate the average bytes/sec over the last 6 progress updates (approx 5 secs) and then check if it is less thanCURLOPT_LOW_SPEED_LIMIT
for at least 5 seconds, thus taking total time of approx. 10 seconds.(1) 关于 PROGRESSFUNCTION 的 Libcurl 文档说:
(2) 您指的是哪个“超时”?我能找到的唯一一个与连接超时有关,这与建立连接后终止连接无关,并且没有发送数据 - 正如您似乎暗示的那样。
(1) Libcurl docs on the PROGRESSFUNCTION say:
(2) Which "timeout" are you referring to? The only one I could find related to a connection timeout, which would not pertain to terminating the connection after it was established, and no data was sent - as you seem to be implying.