关于设置 curl 读写超时的解决方法:
1.C/C++编程实现
curl_easy_setopt(m_curl, CURLOPT_LOW_SPEED_LIMIT,
1L);
curl_easy_setopt(m_curl, CURLOPT_LOW_SPEED_TIME, 5L);
是说: 当 transfer speed 小于 1字节/秒 时 将会在 5秒内 abort 这个 transfer 我的理解 transfer
speed 既可以是下载速度, 也可以是上传速度.
2.命令行实现:
curl -o t "" --speed-time 5 --speed-limit 1
是说: 当传输速度< 1 字节/秒 持续 5 秒时,该连接会终止.
3.curl源码中的实现
CURLOPT_LOW_SPEED_LIMIT 或者 --speed-limit 不可设置为0, 0是无效的, 程序内部实现如下:
lib/url.c:1206:
case CURLOPT_LOW_SPEED_LIMIT:
/*
* The low speed
limit that if transfers are below this for
* CURLOPT_LOW_SPEED_TIME, the
transfer is aborted.
*/
data->set.low_speed_limit=va_arg(param, long);
lib/speedcheck.c:40:
CURLcode
Curl_speedcheck(struct SessionHandle *data, struct timeval
now)
{
if((data->progress.current_speed
>= 0) &&
data->set.low_speed_time &&
(Curl_tvlong(data->state.keeps_speed) != 0) &&
(data->progress.current_speed <
data->set.low_speed_limit)) { /* <--如果 low_speed_limit=0,当时current_speed也为0时,该表达式将始终为假.所以在设置low_speed_limit相关参数时,要大于等于1 */
long howlong =
Curl_tvdiff(now, data->state.keeps_speed);
/*
We are now below the "low speed limit". If we are below it
for "low
speed time" seconds we consider that enough reason
to abort the
download. */
if(
(howlong/1000) > data->set.low_speed_time) {
/*
we have been this slow for long enough, now die
*/
failf(data,
"Operation too
slow. "
"Less than %d bytes/sec
transfered the last %d seconds",
data->set.low_speed_limit,
data->set.low_speed_time);
return
CURLE_OPERATION_TIMEOUTED;
}
Curl_expire(data,
howlong);
} else {
/* we keep up the required speed all right
*/
data->state.keeps_speed =
now;
if(data->set.low_speed_limit)
/* if
there is a low speed limit enabled, we set the expire timer to
make
this connection's speed get checked again no later than when
this
time is up */
Curl_expire(data, data->set.low_speed_time*1000);
}
return CURLE_OK;
}
PS: 源码来自 curl-7.16.2
------------- end -------------
From: GS
-------------------------------
阅读(9662) | 评论(0) | 转发(0) |