From e649432e7234dfe6905f4663bd0ce7b37ef2c5e7 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sun, 14 Apr 2019 23:20:01 +0200 Subject: 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 --- lib/url.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'lib/url.c') diff --git a/lib/url.c b/lib/url.c index 8aefd1583..ad8aa6996 100644 --- a/lib/url.c +++ b/lib/url.c @@ -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; -- cgit v1.2.3