summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2019-04-14 23:20:01 +0200
committerDaniel Stenberg <daniel@haxx.se>2019-04-21 23:06:23 +0200
commite649432e7234dfe6905f4663bd0ce7b37ef2c5e7 (patch)
treeec343333607d509a68fa8ad74cdb9833c1f380ba /lib
parent060f870b85a6ee85668caeb791935fe98f4da56d (diff)
downloadcurl-e649432e7234dfe6905f4663bd0ce7b37ef2c5e7.tar.gz
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
Diffstat (limited to 'lib')
-rw-r--r--lib/conncache.c5
-rw-r--r--lib/setopt.c6
-rw-r--r--lib/url.c23
-rw-r--r--lib/urldata.h3
4 files changed, 34 insertions, 3 deletions
diff --git a/lib/conncache.c b/lib/conncache.c
index 39302ba7b..535091996 100644
--- a/lib/conncache.c
+++ b/lib/conncache.c
@@ -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;
diff --git a/lib/setopt.c b/lib/setopt.c
index 1df38fbb4..594303eff 100644
--- a/lib/setopt.c
+++ b/lib/setopt.c
@@ -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);
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;
diff --git a/lib/urldata.h b/lib/urldata.h
index 22a8e6dda..8f7742082 100644
--- a/lib/urldata.h
+++ b/lib/urldata.h
@@ -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 */