1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-22 08:08:50 -05:00

Avoid an unnecessary call to gettimeofday() when

using custom timeout values.
This commit is contained in:
Yang Tse 2007-04-22 18:17:46 +00:00
parent 0abccc676d
commit 3616912d22
2 changed files with 56 additions and 33 deletions

View File

@ -781,6 +781,7 @@ CURLcode Curl_connecthost(struct connectdata *conn, /* context */
int num_addr; int num_addr;
Curl_addrinfo *ai; Curl_addrinfo *ai;
Curl_addrinfo *curr_addr; Curl_addrinfo *curr_addr;
int timeout_set = 0;
struct timeval after; struct timeval after;
struct timeval before = Curl_tvnow(); struct timeval before = Curl_tvnow();
@ -788,36 +789,39 @@ CURLcode Curl_connecthost(struct connectdata *conn, /* context */
/************************************************************* /*************************************************************
* Figure out what maximum time we have left * Figure out what maximum time we have left
*************************************************************/ *************************************************************/
long timeout_ms= DEFAULT_CONNECT_TIMEOUT; long timeout_ms;
long timeout_per_addr; long timeout_per_addr;
*connected = FALSE; /* default to not connected */ *connected = FALSE; /* default to not connected */
if(data->set.timeout || data->set.connecttimeout) { /* if a timeout is set, use the most restrictive one */
long has_passed;
/* Evaluate in milliseconds how much time that has passed */ if (data->set.timeout > 0)
has_passed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle); timeout_set += 1;
if (data->set.connecttimeout > 0)
timeout_set += 2;
#ifndef min switch (timeout_set) {
#define min(a, b) ((a) < (b) ? (a) : (b)) case 1:
#endif timeout_ms = data->set.timeout;
break;
/* get the most strict timeout of the ones converted to milliseconds */ case 2:
if(data->set.timeout && data->set.connecttimeout) { timeout_ms = data->set.connecttimeout;
if (data->set.timeout < data->set.connecttimeout) break;
timeout_ms = data->set.timeout; case 3:
else if (data->set.timeout < data->set.connecttimeout)
timeout_ms = data->set.connecttimeout;
}
else if(data->set.timeout)
timeout_ms = data->set.timeout; timeout_ms = data->set.timeout;
else else
timeout_ms = data->set.connecttimeout; timeout_ms = data->set.connecttimeout;
break;
default:
timeout_ms = DEFAULT_CONNECT_TIMEOUT;
break;
}
/* subtract the passed time */ if (timeout_set > 0) {
timeout_ms -= has_passed; /* if a timeout was already set, substract elapsed time */
timeout_ms -= Curl_tvdiff(before, data->progress.t_startsingle);
if(timeout_ms < 0) { if(timeout_ms < 0) {
/* a precaution, no need to continue if time already is up */ /* a precaution, no need to continue if time already is up */
failf(data, "Connection time-out"); failf(data, "Connection time-out");

View File

@ -184,27 +184,46 @@ static bool isBadFtpString(const char *string)
*/ */
static CURLcode AllowServerConnect(struct connectdata *conn) static CURLcode AllowServerConnect(struct connectdata *conn)
{ {
int timeout_ms; long timeout_ms;
struct SessionHandle *data = conn->data; struct SessionHandle *data = conn->data;
curl_socket_t sock = conn->sock[SECONDARYSOCKET]; curl_socket_t sock = conn->sock[SECONDARYSOCKET];
struct timeval now = Curl_tvnow(); int timeout_set = 0;
long timespent = Curl_tvdiff(Curl_tvnow(), now);
long timeout = data->set.connecttimeout?data->set.connecttimeout:
(data->set.timeout?data->set.timeout: 0);
if(timeout) { /* if a timeout is set, use the most restrictive one */
timeout -= timespent;
if(timeout<=0) { if (data->set.timeout > 0)
timeout_set += 1;
if (data->set.connecttimeout > 0)
timeout_set += 2;
switch (timeout_set) {
case 1:
timeout_ms = data->set.timeout;
break;
case 2:
timeout_ms = data->set.connecttimeout;
break;
case 3:
if (data->set.timeout < data->set.connecttimeout)
timeout_ms = data->set.timeout;
else
timeout_ms = data->set.connecttimeout;
break;
default:
timeout_ms = 60000; /* 60 seconds default timeout */
break;
}
if (timeout_set > 0) {
/* if a timeout was already set, substract elapsed time */
timeout_ms -= Curl_tvdiff(Curl_tvnow(), conn->now);
if(timeout_ms < 0) {
failf(data, "Timed out before server could connect to us"); failf(data, "Timed out before server could connect to us");
return CURLE_OPERATION_TIMEDOUT; return CURLE_OPERATION_TIMEDOUT;
} }
} }
/* We allow the server 60 seconds to connect to us, or a custom timeout. switch (Curl_socket_ready(sock, CURL_SOCKET_BAD, (int)timeout_ms)) {
Note the typecast here. */
timeout_ms = (timeout?(int)timeout:60000);
switch (Curl_socket_ready(sock, CURL_SOCKET_BAD, timeout_ms)) {
case -1: /* error */ case -1: /* error */
/* let's die here */ /* let's die here */
failf(data, "Error while waiting for server connect"); failf(data, "Error while waiting for server connect");