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);
To set a timeout for a Curl command, you can use the --connect-timeout parameter to set the maximum time in seconds that you allow Curl to connect to the server, or the --max-time (or -m) parameter for the total time in seconds that you authorize the whole operation.
Tell curl with -m / --max-time the maximum time, in seconds, that you allow the command line to spend before curl exits with a timeout error code (28). When the set time has elapsed, curl will exit no matter what is going on at that moment—including if it is transferring data. It really is the maximum time allowed.
The Curl timeout parameter specifies (in seconds) the maximum duration needed for a file/data transfer operation. It is an effective parameter when dealing with issues like links going down or slow networks. This parameter can also take a decimal point value.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With