cURL:空闲超时间隔超过指定值

发布于 2024-10-02 15:06:54 字数 685 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城°AllureLove 2024-10-09 15:06:54

我已经弄清楚了这一点。 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 than CURLOPT_LOW_SPEED_LIMIT for more than CURLOPT_LOW_SPEED_TIME seconds at a stretch, it times out. So if the CURLOPT_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 than CURLOPT_LOW_SPEED_LIMIT for at least 5 seconds, thus taking total time of approx. 10 seconds.

天煞孤星 2024-10-09 15:06:54

(1) 关于 PROGRESSFUNCTION 的 Libcurl 文档说:

该函数由 libcurl 调用
而不是其内部等效项
期间有频繁的间隔
操作(大约每秒一次或
更快)无论数据是否正在
是否已转移。

(2) 您指的是哪个“超时”?我能找到的唯一一个与连接超时有关,这与建立连接后终止连接无关,并且没有发送数据 - 正如您似乎暗示的那样。

(1) Libcurl docs on the PROGRESSFUNCTION say:

This function gets called by libcurl
instead of its internal equivalent
with a frequent interval during
operation (roughly once per second or
sooner) no matter if data is being
transfered or not.

(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文