2012-12-06 19:12:04 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2017-03-29 01:18:24 +08:00
|
|
|
* Copyright (C) 2012 - 2016, Linus Nielsen Feltzing, <linus@haxx.se>
|
2020-01-23 16:03:29 +08:00
|
|
|
* Copyright (C) 2012 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2012-12-06 19:12:04 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-03 07:19:02 +08:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2012-12-06 19:12:04 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-07 02:06:49 +08:00
|
|
|
#include "curl_setup.h"
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "progress.h"
|
|
|
|
#include "multiif.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "conncache.h"
|
2017-11-02 06:37:45 +08:00
|
|
|
#include "share.h"
|
|
|
|
#include "sigpipe.h"
|
|
|
|
#include "connect.h"
|
|
|
|
|
2016-04-29 21:46:40 +08:00
|
|
|
/* The last 3 #include files should be in this order */
|
2015-05-13 05:40:10 +08:00
|
|
|
#include "curl_printf.h"
|
2012-12-06 19:12:04 +08:00
|
|
|
#include "curl_memory.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h"
|
2012-12-06 19:12:04 +08:00
|
|
|
|
2019-05-29 14:59:40 +08:00
|
|
|
#define HASHKEY_SIZE 128
|
|
|
|
|
2015-05-13 05:06:11 +08:00
|
|
|
static void conn_llist_dtor(void *user, void *element)
|
|
|
|
{
|
2018-07-04 06:55:48 +08:00
|
|
|
struct connectdata *conn = element;
|
2015-05-13 05:06:11 +08:00
|
|
|
(void)user;
|
2018-07-04 06:55:48 +08:00
|
|
|
conn->bundle = NULL;
|
2015-05-13 05:06:11 +08:00
|
|
|
}
|
|
|
|
|
2020-04-27 06:33:21 +08:00
|
|
|
static CURLcode bundle_create(struct connectbundle **bundlep)
|
2015-05-13 05:06:11 +08:00
|
|
|
{
|
2020-04-27 06:33:21 +08:00
|
|
|
DEBUGASSERT(*bundlep == NULL);
|
|
|
|
*bundlep = malloc(sizeof(struct connectbundle));
|
|
|
|
if(!*bundlep)
|
2015-05-13 05:06:11 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2020-04-27 06:33:21 +08:00
|
|
|
(*bundlep)->num_connections = 0;
|
|
|
|
(*bundlep)->multiuse = BUNDLE_UNKNOWN;
|
2015-05-13 05:06:11 +08:00
|
|
|
|
2020-04-27 06:33:21 +08:00
|
|
|
Curl_llist_init(&(*bundlep)->conn_list, (curl_llist_dtor) conn_llist_dtor);
|
2015-05-13 05:06:11 +08:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-27 06:33:21 +08:00
|
|
|
static void bundle_destroy(struct connectbundle *bundle)
|
2015-05-13 05:06:11 +08:00
|
|
|
{
|
2020-04-27 06:33:21 +08:00
|
|
|
if(!bundle)
|
2015-05-13 05:06:11 +08:00
|
|
|
return;
|
|
|
|
|
2020-04-27 06:33:21 +08:00
|
|
|
Curl_llist_destroy(&bundle->conn_list, NULL);
|
2017-04-03 16:32:43 +08:00
|
|
|
|
2020-04-27 06:33:21 +08:00
|
|
|
free(bundle);
|
2015-05-13 05:06:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a connection to a bundle */
|
2020-04-27 06:33:21 +08:00
|
|
|
static void bundle_add_conn(struct connectbundle *bundle,
|
2018-06-25 05:22:35 +08:00
|
|
|
struct connectdata *conn)
|
2015-05-13 05:06:11 +08:00
|
|
|
{
|
2020-04-27 06:33:21 +08:00
|
|
|
Curl_llist_insert_next(&bundle->conn_list, bundle->conn_list.tail, conn,
|
2017-04-20 21:10:04 +08:00
|
|
|
&conn->bundle_node);
|
2020-04-27 06:33:21 +08:00
|
|
|
conn->bundle = bundle;
|
|
|
|
bundle->num_connections++;
|
2015-05-13 05:06:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove a connection from a bundle */
|
2020-04-27 06:33:21 +08:00
|
|
|
static int bundle_remove_conn(struct connectbundle *bundle,
|
2015-05-13 05:06:11 +08:00
|
|
|
struct connectdata *conn)
|
|
|
|
{
|
|
|
|
struct curl_llist_element *curr;
|
|
|
|
|
2020-04-27 06:33:21 +08:00
|
|
|
curr = bundle->conn_list.head;
|
2015-05-13 05:06:11 +08:00
|
|
|
while(curr) {
|
|
|
|
if(curr->ptr == conn) {
|
2020-04-27 06:33:21 +08:00
|
|
|
Curl_llist_remove(&bundle->conn_list, curr, NULL);
|
|
|
|
bundle->num_connections--;
|
2015-05-13 05:06:11 +08:00
|
|
|
conn->bundle = NULL;
|
|
|
|
return 1; /* we removed a handle */
|
|
|
|
}
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
2019-12-09 18:53:54 +08:00
|
|
|
DEBUGASSERT(0);
|
2015-05-13 05:06:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-06 19:12:04 +08:00
|
|
|
static void free_bundle_hash_entry(void *freethis)
|
|
|
|
{
|
|
|
|
struct connectbundle *b = (struct connectbundle *) freethis;
|
|
|
|
|
2015-05-13 05:06:11 +08:00
|
|
|
bundle_destroy(b);
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 15:15:02 +08:00
|
|
|
int Curl_conncache_init(struct conncache *connc, int size)
|
2012-12-06 19:12:04 +08:00
|
|
|
{
|
2017-11-02 06:37:45 +08:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* allocate a new easy handle to use when closing cached connections */
|
|
|
|
connc->closure_handle = curl_easy_init();
|
|
|
|
if(!connc->closure_handle)
|
|
|
|
return 1; /* bad */
|
|
|
|
|
|
|
|
rc = Curl_hash_init(&connc->hash, size, Curl_hash_str,
|
|
|
|
Curl_str_key_compare, free_bundle_hash_entry);
|
2019-10-28 16:28:05 +08:00
|
|
|
if(rc)
|
|
|
|
Curl_close(&connc->closure_handle);
|
2017-11-02 06:37:45 +08:00
|
|
|
else
|
|
|
|
connc->closure_handle->state.conn_cache = connc;
|
|
|
|
|
|
|
|
return rc;
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_conncache_destroy(struct conncache *connc)
|
|
|
|
{
|
2015-05-12 15:15:02 +08:00
|
|
|
if(connc)
|
2015-05-03 02:49:55 +08:00
|
|
|
Curl_hash_destroy(&connc->hash);
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
|
2017-03-29 01:18:24 +08:00
|
|
|
/* creates a key to find a bundle for this connection */
|
|
|
|
static void hashkey(struct connectdata *conn, char *buf,
|
2019-05-28 18:14:51 +08:00
|
|
|
size_t len, /* something like 128 is fine */
|
|
|
|
const char **hostp)
|
2015-05-13 05:40:10 +08:00
|
|
|
{
|
2016-01-25 21:37:24 +08:00
|
|
|
const char *hostname;
|
2019-05-28 22:17:51 +08:00
|
|
|
long port = conn->remote_port;
|
2016-01-25 21:37:24 +08:00
|
|
|
|
2019-05-28 22:17:51 +08:00
|
|
|
if(conn->bits.httpproxy && !conn->bits.tunnel_proxy) {
|
2016-11-17 01:49:15 +08:00
|
|
|
hostname = conn->http_proxy.host.name;
|
2019-05-28 22:17:51 +08:00
|
|
|
port = conn->port;
|
|
|
|
}
|
2016-01-25 21:37:24 +08:00
|
|
|
else if(conn->bits.conn_to_host)
|
|
|
|
hostname = conn->conn_to_host.name;
|
|
|
|
else
|
|
|
|
hostname = conn->host.name;
|
|
|
|
|
2019-05-28 18:14:51 +08:00
|
|
|
if(hostp)
|
|
|
|
/* report back which name we used */
|
|
|
|
*hostp = hostname;
|
|
|
|
|
2017-03-29 01:18:24 +08:00
|
|
|
/* put the number first so that the hostname gets cut off if too long */
|
2019-05-28 22:17:51 +08:00
|
|
|
msnprintf(buf, len, "%ld%s", port, hostname);
|
2015-05-13 05:40:10 +08:00
|
|
|
}
|
|
|
|
|
2017-12-02 21:27:00 +08:00
|
|
|
/* 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;
|
2020-04-27 06:33:21 +08:00
|
|
|
CONNCACHE_LOCK(data);
|
2017-12-02 21:27:00 +08:00
|
|
|
num = data->state.conn_cache->num_conn;
|
2020-04-27 06:33:21 +08:00
|
|
|
CONNCACHE_UNLOCK(data);
|
2017-12-02 21:27:00 +08:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2015-05-13 05:06:11 +08:00
|
|
|
/* Look up the bundle with all the connections to the same host this
|
2017-12-02 21:27:00 +08:00
|
|
|
connectdata struct is setup to use.
|
|
|
|
|
|
|
|
**NOTE**: When it returns, it holds the connection cache lock! */
|
2015-03-11 18:54:22 +08:00
|
|
|
struct connectbundle *Curl_conncache_find_bundle(struct connectdata *conn,
|
2019-05-28 18:14:51 +08:00
|
|
|
struct conncache *connc,
|
|
|
|
const char **hostp)
|
2012-12-06 19:12:04 +08:00
|
|
|
{
|
|
|
|
struct connectbundle *bundle = NULL;
|
2020-04-27 06:33:21 +08:00
|
|
|
CONNCACHE_LOCK(conn->data);
|
2015-05-13 05:40:10 +08:00
|
|
|
if(connc) {
|
2019-05-29 14:59:40 +08:00
|
|
|
char key[HASHKEY_SIZE];
|
2019-05-28 18:14:51 +08:00
|
|
|
hashkey(conn, key, sizeof(key), hostp);
|
2017-03-29 01:18:24 +08:00
|
|
|
bundle = Curl_hash_pick(&connc->hash, key, strlen(key));
|
2015-05-13 05:40:10 +08:00
|
|
|
}
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
return bundle;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool conncache_add_bundle(struct conncache *connc,
|
2015-05-13 05:40:10 +08:00
|
|
|
char *key,
|
2012-12-06 19:12:04 +08:00
|
|
|
struct connectbundle *bundle)
|
|
|
|
{
|
2015-05-13 05:40:10 +08:00
|
|
|
void *p = Curl_hash_add(&connc->hash, key, strlen(key), bundle);
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
return p?TRUE:FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void conncache_remove_bundle(struct conncache *connc,
|
|
|
|
struct connectbundle *bundle)
|
|
|
|
{
|
|
|
|
struct curl_hash_iterator iter;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
|
|
|
|
if(!connc)
|
|
|
|
return;
|
|
|
|
|
2015-05-12 15:15:02 +08:00
|
|
|
Curl_hash_start_iterate(&connc->hash, &iter);
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
|
|
|
if(he->ptr == bundle) {
|
|
|
|
/* The bundle is destroyed by the hash destructor function,
|
|
|
|
free_bundle_hash_entry() */
|
2015-05-12 15:15:02 +08:00
|
|
|
Curl_hash_delete(&connc->hash, he->key, he->key_len);
|
2012-12-06 19:12:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode Curl_conncache_add_conn(struct conncache *connc,
|
|
|
|
struct connectdata *conn)
|
|
|
|
{
|
2018-06-25 05:22:35 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
2020-04-27 06:33:21 +08:00
|
|
|
struct connectbundle *bundle = NULL;
|
2016-06-21 21:47:12 +08:00
|
|
|
struct Curl_easy *data = conn->data;
|
2012-12-06 19:12:04 +08:00
|
|
|
|
2017-12-02 21:27:00 +08:00
|
|
|
/* *find_bundle() locks the connection cache */
|
2019-05-28 18:14:51 +08:00
|
|
|
bundle = Curl_conncache_find_bundle(conn, data->state.conn_cache, NULL);
|
2012-12-06 19:12:04 +08:00
|
|
|
if(!bundle) {
|
2015-05-13 05:40:10 +08:00
|
|
|
int rc;
|
2019-05-29 14:59:40 +08:00
|
|
|
char key[HASHKEY_SIZE];
|
2015-03-11 18:54:22 +08:00
|
|
|
|
2020-04-27 06:33:21 +08:00
|
|
|
result = bundle_create(&bundle);
|
2017-12-02 21:27:00 +08:00
|
|
|
if(result) {
|
|
|
|
goto unlock;
|
|
|
|
}
|
2012-12-06 19:12:04 +08:00
|
|
|
|
2019-05-28 18:14:51 +08:00
|
|
|
hashkey(conn, key, sizeof(key), NULL);
|
2020-04-27 06:33:21 +08:00
|
|
|
rc = conncache_add_bundle(data->state.conn_cache, key, bundle);
|
2017-03-29 01:18:24 +08:00
|
|
|
|
2015-05-13 05:40:10 +08:00
|
|
|
if(!rc) {
|
2020-04-27 06:33:21 +08:00
|
|
|
bundle_destroy(bundle);
|
2017-12-02 21:27:00 +08:00
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto unlock;
|
2012-12-20 02:52:11 +08:00
|
|
|
}
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
|
2018-06-25 05:22:35 +08:00
|
|
|
bundle_add_conn(bundle, conn);
|
2014-06-13 02:36:41 +08:00
|
|
|
conn->connection_id = connc->next_connection_id++;
|
2017-12-02 21:27:00 +08:00
|
|
|
connc->num_conn++;
|
2014-11-19 06:02:40 +08:00
|
|
|
|
2014-11-20 04:04:21 +08:00
|
|
|
DEBUGF(infof(conn->data, "Added connection %ld. "
|
2018-05-29 03:46:22 +08:00
|
|
|
"The cache now contains %zu members\n",
|
|
|
|
conn->connection_id, connc->num_conn));
|
2017-12-02 21:27:00 +08:00
|
|
|
|
|
|
|
unlock:
|
2020-04-27 06:33:21 +08:00
|
|
|
CONNCACHE_UNLOCK(data);
|
2012-12-06 19:12:04 +08:00
|
|
|
|
2017-12-13 06:53:56 +08:00
|
|
|
return result;
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
|
2018-12-20 23:39:02 +08:00
|
|
|
/*
|
2020-04-27 06:33:21 +08:00
|
|
|
* Removes the connectdata object from the connection cache, but does *not*
|
|
|
|
* clear the conn->data association. The transfer still owns this connection.
|
|
|
|
*
|
|
|
|
* Pass TRUE/FALSE in the 'lock' argument depending on if the parent function
|
|
|
|
* already holds the lock or not.
|
2018-12-20 23:39:02 +08:00
|
|
|
*/
|
|
|
|
void Curl_conncache_remove_conn(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn, bool lock)
|
2012-12-06 19:12:04 +08:00
|
|
|
{
|
|
|
|
struct connectbundle *bundle = conn->bundle;
|
2017-12-02 21:27:00 +08:00
|
|
|
struct conncache *connc = data->state.conn_cache;
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
/* The bundle pointer can be NULL, since this function can be called
|
|
|
|
due to a failed connection attempt, before being added to a bundle */
|
|
|
|
if(bundle) {
|
2017-12-02 21:27:00 +08:00
|
|
|
if(lock) {
|
2020-04-27 06:33:21 +08:00
|
|
|
CONNCACHE_LOCK(data);
|
2017-12-02 21:27:00 +08:00
|
|
|
}
|
2015-05-13 05:06:11 +08:00
|
|
|
bundle_remove_conn(bundle, conn);
|
2017-11-02 06:37:45 +08:00
|
|
|
if(bundle->num_connections == 0)
|
2012-12-06 19:12:04 +08:00
|
|
|
conncache_remove_bundle(connc, bundle);
|
2017-12-02 21:27:00 +08:00
|
|
|
conn->bundle = NULL; /* removed from it */
|
2014-01-04 23:34:58 +08:00
|
|
|
if(connc) {
|
2017-12-02 21:27:00 +08:00
|
|
|
connc->num_conn--;
|
2018-07-04 06:55:48 +08:00
|
|
|
DEBUGF(infof(data, "The cache now contains %zu members\n",
|
2018-05-29 03:46:22 +08:00
|
|
|
connc->num_conn));
|
2017-12-02 21:27:00 +08:00
|
|
|
}
|
|
|
|
if(lock) {
|
2020-04-27 06:33:21 +08:00
|
|
|
CONNCACHE_UNLOCK(data);
|
2014-01-04 23:34:58 +08:00
|
|
|
}
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 21:27:00 +08:00
|
|
|
/* This function iterates the entire connection cache and calls the function
|
|
|
|
func() with the connection pointer as the first argument and the supplied
|
|
|
|
'param' argument as the other.
|
|
|
|
|
|
|
|
The conncache lock is still held when the callback is called. It needs it,
|
|
|
|
so that it can safely continue traversing the lists once the callback
|
|
|
|
returns.
|
|
|
|
|
|
|
|
Returns 1 if the loop was aborted due to the callback's return code.
|
2012-12-12 03:13:29 +08:00
|
|
|
|
|
|
|
Return 0 from func() to continue the loop, return 1 to abort it.
|
|
|
|
*/
|
2017-12-02 21:27:00 +08:00
|
|
|
bool Curl_conncache_foreach(struct Curl_easy *data,
|
2017-11-02 06:37:45 +08:00
|
|
|
struct conncache *connc,
|
2012-12-06 19:12:04 +08:00
|
|
|
void *param,
|
2012-12-12 03:13:29 +08:00
|
|
|
int (*func)(struct connectdata *conn, void *param))
|
2012-12-06 19:12:04 +08:00
|
|
|
{
|
|
|
|
struct curl_hash_iterator iter;
|
|
|
|
struct curl_llist_element *curr;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
|
|
|
|
if(!connc)
|
2017-12-02 21:27:00 +08:00
|
|
|
return FALSE;
|
2012-12-06 19:12:04 +08:00
|
|
|
|
2020-04-27 06:33:21 +08:00
|
|
|
CONNCACHE_LOCK(data);
|
2015-05-12 15:15:02 +08:00
|
|
|
Curl_hash_start_iterate(&connc->hash, &iter);
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
|
|
|
struct connectbundle *bundle;
|
|
|
|
|
|
|
|
bundle = he->ptr;
|
2014-08-11 06:06:20 +08:00
|
|
|
he = Curl_hash_next_element(&iter);
|
2012-12-06 19:12:04 +08:00
|
|
|
|
2017-04-03 16:32:43 +08:00
|
|
|
curr = bundle->conn_list.head;
|
2012-12-06 19:12:04 +08:00
|
|
|
while(curr) {
|
|
|
|
/* Yes, we need to update curr before calling func(), because func()
|
|
|
|
might decide to remove the connection */
|
2014-10-14 15:44:06 +08:00
|
|
|
struct connectdata *conn = curr->ptr;
|
2012-12-06 19:12:04 +08:00
|
|
|
curr = curr->next;
|
|
|
|
|
2017-11-02 06:37:45 +08:00
|
|
|
if(1 == func(conn, param)) {
|
2020-04-27 06:33:21 +08:00
|
|
|
CONNCACHE_UNLOCK(data);
|
2017-12-02 21:27:00 +08:00
|
|
|
return TRUE;
|
2017-11-02 06:37:45 +08:00
|
|
|
}
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-27 06:33:21 +08:00
|
|
|
CONNCACHE_UNLOCK(data);
|
2017-12-02 21:27:00 +08:00
|
|
|
return FALSE;
|
2012-12-06 19:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the first connection found in the cache. Used when closing all
|
2017-11-02 06:37:45 +08:00
|
|
|
connections.
|
|
|
|
|
|
|
|
NOTE: no locking is done here as this is presumably only done when cleaning
|
|
|
|
up a cache!
|
|
|
|
*/
|
2019-02-08 16:33:42 +08:00
|
|
|
static struct connectdata *
|
|
|
|
conncache_find_first_connection(struct conncache *connc)
|
2012-12-06 19:12:04 +08:00
|
|
|
{
|
|
|
|
struct curl_hash_iterator iter;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
struct connectbundle *bundle;
|
|
|
|
|
2015-05-12 15:15:02 +08:00
|
|
|
Curl_hash_start_iterate(&connc->hash, &iter);
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
2014-10-14 15:44:06 +08:00
|
|
|
struct curl_llist_element *curr;
|
2012-12-06 19:12:04 +08:00
|
|
|
bundle = he->ptr;
|
|
|
|
|
2017-04-03 16:32:43 +08:00
|
|
|
curr = bundle->conn_list.head;
|
2012-12-06 19:12:04 +08:00
|
|
|
if(curr) {
|
|
|
|
return curr->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-11-02 06:37:45 +08:00
|
|
|
/*
|
2017-12-02 21:27:00 +08:00
|
|
|
* Give ownership of a connection back to the connection cache. Might
|
|
|
|
* disconnect the oldest existing in there to make space.
|
|
|
|
*
|
|
|
|
* Return TRUE if stored, FALSE if closed.
|
|
|
|
*/
|
2019-12-09 18:53:54 +08:00
|
|
|
bool Curl_conncache_return_conn(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn)
|
2017-12-02 21:27:00 +08:00
|
|
|
{
|
|
|
|
/* data->multi->maxconnects can be negative, deal with it. */
|
|
|
|
size_t maxconnects =
|
|
|
|
(data->multi->maxconnects < 0) ? data->multi->num_easy * 4:
|
|
|
|
data->multi->maxconnects;
|
|
|
|
struct connectdata *conn_candidate = NULL;
|
|
|
|
|
2019-04-15 05:20:01 +08:00
|
|
|
conn->lastused = Curl_now(); /* it was used up until now */
|
2017-12-02 21:27:00 +08:00
|
|
|
if(maxconnects > 0 &&
|
|
|
|
Curl_conncache_size(data) > maxconnects) {
|
|
|
|
infof(data, "Connection cache is full, closing the oldest one.\n");
|
|
|
|
|
|
|
|
conn_candidate = Curl_conncache_extract_oldest(data);
|
|
|
|
if(conn_candidate) {
|
|
|
|
/* the winner gets the honour of being disconnected */
|
2018-07-04 06:55:48 +08:00
|
|
|
(void)Curl_disconnect(data, conn_candidate, /* dead_connection */ FALSE);
|
2017-12-02 21:27:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (conn_candidate == conn) ? FALSE : TRUE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function finds the connection in the connection bundle that has been
|
|
|
|
* unused for the longest time.
|
|
|
|
*
|
|
|
|
* Does not lock the connection cache!
|
2017-11-02 06:37:45 +08:00
|
|
|
*
|
|
|
|
* Returns the pointer to the oldest idle connection, or NULL if none was
|
|
|
|
* found.
|
|
|
|
*/
|
|
|
|
struct connectdata *
|
2017-12-02 21:27:00 +08:00
|
|
|
Curl_conncache_extract_bundle(struct Curl_easy *data,
|
|
|
|
struct connectbundle *bundle)
|
|
|
|
{
|
|
|
|
struct curl_llist_element *curr;
|
|
|
|
timediff_t highscore = -1;
|
|
|
|
timediff_t score;
|
|
|
|
struct curltime now;
|
|
|
|
struct connectdata *conn_candidate = NULL;
|
|
|
|
struct connectdata *conn;
|
|
|
|
|
|
|
|
(void)data;
|
|
|
|
|
|
|
|
now = Curl_now();
|
|
|
|
|
|
|
|
curr = bundle->conn_list.head;
|
|
|
|
while(curr) {
|
|
|
|
conn = curr->ptr;
|
|
|
|
|
2019-03-17 07:49:21 +08:00
|
|
|
if(!CONN_INUSE(conn) && !conn->data) {
|
2017-12-02 21:27:00 +08:00
|
|
|
/* Set higher score for the age passed since the connection was used */
|
2019-04-15 05:20:01 +08:00
|
|
|
score = Curl_timediff(now, conn->lastused);
|
2017-12-02 21:27:00 +08:00
|
|
|
|
|
|
|
if(score > highscore) {
|
|
|
|
highscore = score;
|
|
|
|
conn_candidate = conn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
if(conn_candidate) {
|
|
|
|
/* remove it to prevent another thread from nicking it */
|
|
|
|
bundle_remove_conn(bundle, conn_candidate);
|
|
|
|
data->state.conn_cache->num_conn--;
|
2018-05-29 03:46:22 +08:00
|
|
|
DEBUGF(infof(data, "The cache now contains %zu members\n",
|
|
|
|
data->state.conn_cache->num_conn));
|
2018-07-04 06:55:48 +08:00
|
|
|
conn_candidate->data = data; /* associate! */
|
2017-12-02 21:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return conn_candidate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function finds the connection in the connection cache that has been
|
|
|
|
* unused for the longest time and extracts that from the bundle.
|
|
|
|
*
|
|
|
|
* Returns the pointer to the connection, or NULL if none was found.
|
|
|
|
*/
|
|
|
|
struct connectdata *
|
|
|
|
Curl_conncache_extract_oldest(struct Curl_easy *data)
|
2017-11-02 06:37:45 +08:00
|
|
|
{
|
2017-12-02 21:27:00 +08:00
|
|
|
struct conncache *connc = data->state.conn_cache;
|
2017-11-02 06:37:45 +08:00
|
|
|
struct curl_hash_iterator iter;
|
|
|
|
struct curl_llist_element *curr;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
timediff_t highscore =- 1;
|
|
|
|
timediff_t score;
|
|
|
|
struct curltime now;
|
|
|
|
struct connectdata *conn_candidate = NULL;
|
|
|
|
struct connectbundle *bundle;
|
2017-12-02 21:27:00 +08:00
|
|
|
struct connectbundle *bundle_candidate = NULL;
|
2017-11-02 06:37:45 +08:00
|
|
|
|
|
|
|
now = Curl_now();
|
|
|
|
|
2020-04-27 06:33:21 +08:00
|
|
|
CONNCACHE_LOCK(data);
|
2017-12-02 21:27:00 +08:00
|
|
|
Curl_hash_start_iterate(&connc->hash, &iter);
|
2017-11-02 06:37:45 +08:00
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
|
|
|
struct connectdata *conn;
|
|
|
|
|
|
|
|
bundle = he->ptr;
|
|
|
|
|
|
|
|
curr = bundle->conn_list.head;
|
|
|
|
while(curr) {
|
|
|
|
conn = curr->ptr;
|
|
|
|
|
2019-12-10 17:39:16 +08:00
|
|
|
if(!CONN_INUSE(conn) && !conn->data && !conn->bits.close &&
|
|
|
|
!conn->bits.connect_only) {
|
2017-11-02 06:37:45 +08:00
|
|
|
/* Set higher score for the age passed since the connection was used */
|
2019-04-15 05:20:01 +08:00
|
|
|
score = Curl_timediff(now, conn->lastused);
|
2017-11-02 06:37:45 +08:00
|
|
|
|
|
|
|
if(score > highscore) {
|
|
|
|
highscore = score;
|
|
|
|
conn_candidate = conn;
|
2017-12-02 21:27:00 +08:00
|
|
|
bundle_candidate = bundle;
|
2017-11-02 06:37:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
}
|
2017-12-02 21:27:00 +08:00
|
|
|
if(conn_candidate) {
|
|
|
|
/* remove it to prevent another thread from nicking it */
|
|
|
|
bundle_remove_conn(bundle_candidate, conn_candidate);
|
|
|
|
connc->num_conn--;
|
2018-05-29 03:46:22 +08:00
|
|
|
DEBUGF(infof(data, "The cache now contains %zu members\n",
|
|
|
|
connc->num_conn));
|
2018-07-04 06:55:48 +08:00
|
|
|
conn_candidate->data = data; /* associate! */
|
2017-12-02 21:27:00 +08:00
|
|
|
}
|
2020-04-27 06:33:21 +08:00
|
|
|
CONNCACHE_UNLOCK(data);
|
2017-11-02 06:37:45 +08:00
|
|
|
|
|
|
|
return conn_candidate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_conncache_close_all_connections(struct conncache *connc)
|
|
|
|
{
|
|
|
|
struct connectdata *conn;
|
|
|
|
|
2019-02-08 16:33:42 +08:00
|
|
|
conn = conncache_find_first_connection(connc);
|
2017-11-02 06:37:45 +08:00
|
|
|
while(conn) {
|
|
|
|
SIGPIPE_VARIABLE(pipe_st);
|
|
|
|
conn->data = connc->closure_handle;
|
|
|
|
|
|
|
|
sigpipe_ignore(conn->data, &pipe_st);
|
|
|
|
/* This will remove the connection from the cache */
|
|
|
|
connclose(conn, "kill all");
|
2020-04-27 06:33:21 +08:00
|
|
|
Curl_conncache_remove_conn(conn->data, conn, TRUE);
|
2018-07-04 06:55:48 +08:00
|
|
|
(void)Curl_disconnect(connc->closure_handle, conn, FALSE);
|
2017-11-02 06:37:45 +08:00
|
|
|
sigpipe_restore(&pipe_st);
|
|
|
|
|
2019-02-08 16:33:42 +08:00
|
|
|
conn = conncache_find_first_connection(connc);
|
2017-11-02 06:37:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(connc->closure_handle) {
|
|
|
|
SIGPIPE_VARIABLE(pipe_st);
|
|
|
|
sigpipe_ignore(connc->closure_handle, &pipe_st);
|
|
|
|
|
|
|
|
Curl_hostcache_clean(connc->closure_handle,
|
|
|
|
connc->closure_handle->dns.hostcache);
|
2019-10-28 16:28:05 +08:00
|
|
|
Curl_close(&connc->closure_handle);
|
2017-11-02 06:37:45 +08:00
|
|
|
sigpipe_restore(&pipe_st);
|
|
|
|
}
|
|
|
|
}
|
2012-12-06 19:12:04 +08:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Useful for debugging the connection cache */
|
|
|
|
void Curl_conncache_print(struct conncache *connc)
|
|
|
|
{
|
|
|
|
struct curl_hash_iterator iter;
|
|
|
|
struct curl_llist_element *curr;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
|
|
|
|
if(!connc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf(stderr, "=Bundle cache=\n");
|
|
|
|
|
|
|
|
Curl_hash_start_iterate(connc->hash, &iter);
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
|
|
|
struct connectbundle *bundle;
|
|
|
|
struct connectdata *conn;
|
|
|
|
|
|
|
|
bundle = he->ptr;
|
|
|
|
|
|
|
|
fprintf(stderr, "%s -", he->key);
|
|
|
|
curr = bundle->conn_list->head;
|
|
|
|
while(curr) {
|
|
|
|
conn = curr->ptr;
|
|
|
|
|
|
|
|
fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|