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

@ -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;