summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2017-12-04 22:52:08 +0100
committerDaniel Stenberg <daniel@haxx.se>2017-12-04 22:52:08 +0100
commit63f6772b0c1c3f91743ecc39fa70ebd36694f260 (patch)
treee4c2c80268bd94d89a7c1c9741587bd4c56659dd
parentf24996d71ad976022df4452e7ca398bd52def398 (diff)
downloadcurl-63f6772b0c1c3f91743ecc39fa70ebd36694f260.tar.gz
conncache: provide a function to return the size, num of connections
-rw-r--r--lib/conncache.c23
-rw-r--r--lib/conncache.h10
-rw-r--r--lib/multi.c2
-rw-r--r--lib/url.c4
4 files changed, 30 insertions, 9 deletions
diff --git a/lib/conncache.c b/lib/conncache.c
index eef00555e..51a279999 100644
--- a/lib/conncache.c
+++ b/lib/conncache.c
@@ -54,12 +54,13 @@
(x)->state.conncache_lock = FALSE; \
Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT); \
}
-
+#define IS_LOCKED(x) DEBUGASSERT((x)->state.conncache_lock)
#else
#define CONN_LOCK(x) if((x)->share) \
Curl_share_lock((x), CURL_LOCK_DATA_CONNECT, CURL_LOCK_ACCESS_SINGLE)
#define CONN_UNLOCK(x) if((x)->share) \
Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT)
+#define IS_LOCKED(x)
#endif
static void conn_llist_dtor(void *user, void *element)
@@ -186,6 +187,18 @@ void Curl_conncache_unlock(struct connectdata *conn)
CONN_UNLOCK(conn->data);
}
+/* Returns number of connections currently held in the connection cache.
+ Locks/unlocks the cache itself!
+*/
+size_t Curl_conncache_size(struct Curl_easy *data)
+{
+ size_t num;
+ CONN_LOCK(data);
+ num = data->state.conn_cache->num_conn;
+ CONN_UNLOCK(data);
+ return num;
+}
+
/* Look up the bundle with all the connections to the same host this
connectdata struct is setup to use.
@@ -275,11 +288,11 @@ CURLcode Curl_conncache_add_conn(struct conncache *connc,
}
conn->connection_id = connc->next_connection_id++;
- connc->num_connections++;
+ connc->num_conn++;
DEBUGF(infof(conn->data, "Added connection %ld. "
"The cache now contains %" CURL_FORMAT_CURL_OFF_TU " members\n",
- conn->connection_id, (curl_off_t) connc->num_connections));
+ conn->connection_id, (curl_off_t) connc->num_conn));
unlock:
CONN_UNLOCK(data);
@@ -300,10 +313,10 @@ void Curl_conncache_remove_conn(struct conncache *connc,
if(bundle->num_connections == 0)
conncache_remove_bundle(connc, bundle);
if(connc) {
- connc->num_connections--;
+ connc->num_conn--;
DEBUGF(infof(conn->data, "The cache now contains %"
CURL_FORMAT_CURL_OFF_TU " members\n",
- (curl_off_t) connc->num_connections));
+ (curl_off_t) connc->num_conn));
}
CONN_UNLOCK(conn->data);
}
diff --git a/lib/conncache.h b/lib/conncache.h
index 6a1ffecdd..af29441d2 100644
--- a/lib/conncache.h
+++ b/lib/conncache.h
@@ -23,9 +23,15 @@
*
***************************************************************************/
+/*
+ * All accesses to struct fields and changing of data in the connection cache
+ * and connectbundles must be done with the conncache LOCKED. The cache might
+ * be shared.
+ */
+
struct conncache {
struct curl_hash hash;
- size_t num_connections;
+ size_t num_conn;
long next_connection_id;
struct curltime last_cleanup;
/* handle used for closing cached connections */
@@ -51,6 +57,8 @@ void Curl_conncache_destroy(struct conncache *connc);
struct connectbundle *Curl_conncache_find_bundle(struct connectdata *conn,
struct conncache *connc);
void Curl_conncache_unlock(struct connectdata *conn);
+/* returns number of connections currently held in the connection cache */
+size_t Curl_conncache_size(struct Curl_easy *data);
CURLcode Curl_conncache_add_conn(struct conncache *connc,
struct connectdata *conn);
diff --git a/lib/multi.c b/lib/multi.c
index 9728e5a2f..6e092a136 100644
--- a/lib/multi.c
+++ b/lib/multi.c
@@ -494,7 +494,7 @@ ConnectionDone(struct Curl_easy *data, struct connectdata *conn)
conn->inuse = FALSE;
if(maxconnects > 0 &&
- data->state.conn_cache->num_connections > maxconnects) {
+ Curl_conncache_size(data) > maxconnects) {
infof(data, "Connection cache is full, closing the oldest one.\n");
conn_candidate = Curl_conncache_oldest_idle(data);
diff --git a/lib/url.c b/lib/url.c
index ca48c56b3..6c2a4d4f1 100644
--- a/lib/url.c
+++ b/lib/url.c
@@ -4420,7 +4420,7 @@ static CURLcode create_conn(struct Curl_easy *data,
conn_temp->connection_id, pipelen);
if(conn_temp->bundle->num_connections < max_host_connections &&
- data->state.conn_cache->num_connections < max_total_connections) {
+ Curl_conncache_size(data) < max_total_connections) {
/* We want a new connection anyway */
reuse = FALSE;
@@ -4499,7 +4499,7 @@ static CURLcode create_conn(struct Curl_easy *data,
if(connections_available &&
(max_total_connections > 0) &&
- (data->state.conn_cache->num_connections >= max_total_connections)) {
+ (Curl_conncache_size(data) >= max_total_connections)) {
struct connectdata *conn_candidate;
/* The cache is full. Let's see if we can kill a connection. */