CURLOPT_MAXAGE_CONN: set the maximum allowed age for conn reuse

... and disconnect too old ones instead of trying to reuse.

Default max age is set to 118 seconds.

Ref: #3722
Closes #3782
This commit is contained in:
Daniel Stenberg 2019-04-14 23:20:01 +02:00
parent 060f870b85
commit e649432e72
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
10 changed files with 106 additions and 14 deletions

View file

@ -434,6 +434,7 @@ bool Curl_conncache_return_conn(struct connectdata *conn)
struct connectdata *conn_candidate = NULL;
conn->data = NULL; /* no owner anymore */
conn->lastused = Curl_now(); /* it was used up until now */
if(maxconnects > 0 &&
Curl_conncache_size(data) > maxconnects) {
infof(data, "Connection cache is full, closing the oldest one.\n");
@ -479,7 +480,7 @@ Curl_conncache_extract_bundle(struct Curl_easy *data,
if(!CONN_INUSE(conn) && !conn->data) {
/* Set higher score for the age passed since the connection was used */
score = Curl_timediff(now, conn->now);
score = Curl_timediff(now, conn->lastused);
if(score > highscore) {
highscore = score;
@ -537,7 +538,7 @@ Curl_conncache_extract_oldest(struct Curl_easy *data)
if(!CONN_INUSE(conn) && !conn->data) {
/* Set higher score for the age passed since the connection was used */
score = Curl_timediff(now, conn->now);
score = Curl_timediff(now, conn->lastused);
if(score > highscore) {
highscore = score;

View file

@ -2645,6 +2645,12 @@ static CURLcode vsetopt(struct Curl_easy *data, CURLoption option,
return CURLE_BAD_FUNCTION_ARGUMENT;
data->set.upkeep_interval_ms = arg;
break;
case CURLOPT_MAXAGE_CONN:
arg = va_arg(param, long);
if(arg < 0)
return CURLE_BAD_FUNCTION_ARGUMENT;
data->set.maxage_conn = arg;
break;
case CURLOPT_TRAILERFUNCTION:
#ifndef CURL_DISABLE_HTTP
data->set.trailer_callback = va_arg(param, curl_trailer_callback);

View file

@ -541,6 +541,7 @@ CURLcode Curl_init_userdefined(struct Curl_easy *data)
set->fnmatch = ZERO_NULL;
set->upkeep_interval_ms = CURL_UPKEEP_INTERVAL_DEFAULT;
set->maxconnects = DEFAULT_CONNCACHE_SIZE; /* for easy handles */
set->maxage_conn = 118;
set->http09_allowed = TRUE;
set->httpversion =
#ifdef USE_NGHTTP2
@ -958,6 +959,25 @@ static void prune_dead_connections(struct Curl_easy *data)
}
}
/* A connection has to have been idle for a shorter time than 'maxage_conn' to
be subject for reuse. The success rate is just too low after this. */
static bool conn_maxage(struct Curl_easy *data,
struct connectdata *conn,
struct curltime now)
{
if(!conn->data) {
timediff_t idletime = Curl_timediff(now, conn->lastused);
idletime /= 1000; /* integer seconds is fine */
if(idletime/1000 > data->set.maxage_conn) {
infof(data, "Too old connection (%ld seconds), disconnect it\n",
idletime);
return TRUE;
}
}
return FALSE;
}
/*
* Given one filled in connection struct (named needle), this function should
* detect if there already is one that has all the significant details
@ -981,6 +1001,7 @@ ConnectionExists(struct Curl_easy *data,
bool foundPendingCandidate = FALSE;
bool canmultiplex = IsMultiplexingPossible(data, needle);
struct connectbundle *bundle;
struct curltime now = Curl_now();
#ifdef USE_NTLM
bool wantNTLMhttp = ((data->state.authhost.want &
@ -1044,7 +1065,7 @@ ConnectionExists(struct Curl_easy *data,
/* connect-only connections will not be reused */
continue;
if(extract_if_dead(check, data)) {
if(conn_maxage(data, check, now) || extract_if_dead(check, data)) {
/* disconnect it */
(void)Curl_disconnect(data, check, /* dead_connection */TRUE);
continue;

View file

@ -866,6 +866,7 @@ struct connectdata {
struct curltime now; /* "current" time */
struct curltime created; /* creation time */
struct curltime lastused; /* when returned to the connection cache */
curl_socket_t sock[2]; /* two sockets, the second is used for the data
transfer when doing FTP */
curl_socket_t tempsock[2]; /* temporary sockets for happy eyeballs */
@ -1553,6 +1554,8 @@ struct UserDefined {
long accepttimeout; /* in milliseconds, 0 means no timeout */
long happy_eyeballs_timeout; /* in milliseconds, 0 is a valid value */
long server_response_timeout; /* in milliseconds, 0 means no timeout */
long maxage_conn; /* in seconds, max idle time to allow a connection that
is to be reused */
long tftp_blksize; /* in bytes, 0 means use default */
curl_off_t filesize; /* size of file to upload, -1 means unknown */
long low_speed_limit; /* bytes/second */