Paul J. Lucas
Paul J. Lucas

Reputation: 7131

Why alter curl_multi_timeout() return value?

This sample code contains:

curl_multi_timeout(multi_handle, &curl_timeo);
if(curl_timeo >= 0) {
  timeout.tv_sec = curl_timeo / 1000;
  if(timeout.tv_sec > 1)
    timeout.tv_sec = 1;
  else
    timeout.tv_usec = (curl_timeo % 1000) * 1000;
}

Why is tv_sec clipped to 1 second? Why isn't the value returned by curl_multi_timeout() used as-is (after dividing by 1000)?

Assuming there's a good reason for the above, then is there a case when you would NOT clip the value to 1 second? What case is that?

Upvotes: 1

Views: 1246

Answers (1)

unpythonic
unpythonic

Reputation: 4070

The code is just setting a maximum wait time for the later call to select(). If anything, this looks like a bug. It looks as if the code is protecting itself from an unreasonable answer from curl_multi_timeout(). My guess is that the coder was thinking, "if the curl timeout function returns something longer than one minute, then don't wait any longer than that." ...and then proceeded to typo one minute as one second. It probably should be doing

if (timeout.tv_sec > 60) {
    timeout.tv_sec = 60;
else if (timeout.tv_sec == 0) {
    timeout.tv_usec = curl_timeo * 1000;
}

The mod by 1000 is unnecessary since curl_multi_timeout() returns milliseconds, so if tv_sec is zero, that means that the returned value is in the range of 0 - 999.

Upvotes: 1

Related Questions