mirror of
https://github.com/curl/curl.git
synced 2024-11-27 05:50:21 +08:00
cpool: rename "connection cache/conncache" to "Connection Pools/cpool"
This is a better match for what they do and the general "cpool" var/function prefix works well. The pool now handles very long hostnames correctly. The following changes have been made: * 'struct connectdata', e.g. connections, keep new members named `destination` and ' destination_len' that fully specifies interface+port+hostname of where the connection is going to. This is used in the pool for "bundling" of connections with the same destination. There is no limit on the length any more. * Locking: all locks are done inside conncache.c when calling into the pool and released on return. This eliminates hazards of the callers keeping track. * 'struct connectbundle' is now internal to the pool. It is no longer referenced by a connection. * 'bundle->multiuse' no longer exists. HTTP/2 and 3 and TLS filters no longer need to set it. Instead, the multi checks on leaving MSTATE_CONNECT or MSTATE_CONNECTING if the connection is now multiplexed and new, e.g. not conn->bits.reuse. In that case the processing of pending handles is triggered. * The pool's init is provided with a callback to invoke on all connections being discarded. This allows the cleanups in `Curl_disconnect` to run, wherever it is decided to retire a connection. * Several pool operations can now be fully done with one call. Pruning dead connections, upkeep and checks on pool limits can now directly discard connections and need no longer return those to the caller for doing that (as we have now the callback described above). * Finding a connection for reuse is now done via `Curl_cpool_find()` and the caller provides callbacks to evaluate the connection candidates. * The 'Curl_cpool_check_limits()' now directly uses the max values that may be set in the transfer's multi. No need to pass them around. Curl_multi_max_host_connections() and Curl_multi_max_total_connections() are gone. * Add method 'Curl_node_llist()' to get the llist a node is in. Used in cpool to verify connection are indeed in the list (or not in any list) as they need to. I left the conncache.[ch] as is for now and also did not touch the documentation. If we update that outside the feature window, we can do this in a separate PR. Multi-thread safety is not achieved by this PR, but since more details on how pools operate are now "internal" it is a better starting point to go for this in the future. Closes #14662
This commit is contained in:
parent
3af75e18d6
commit
1be704e17e
1185
lib/conncache.c
1185
lib/conncache.c
File diff suppressed because it is too large
Load Diff
247
lib/conncache.h
247
lib/conncache.h
@ -25,140 +25,177 @@
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include "timeval.h"
|
||||
|
||||
struct connectdata;
|
||||
struct Curl_easy;
|
||||
struct curl_pollfds;
|
||||
struct curl_waitfds;
|
||||
struct Curl_multi;
|
||||
struct Curl_share;
|
||||
|
||||
struct connshutdowns {
|
||||
struct Curl_llist conn_list; /* The connectdata to shut down */
|
||||
BIT(iter_locked); /* TRUE while iterating the list */
|
||||
};
|
||||
/**
|
||||
* Callback invoked when disconnecting connections.
|
||||
* @param data transfer last handling the connection, not attached
|
||||
* @param conn the connection to discard
|
||||
* @param aborted if the connection is being aborted
|
||||
* @return if the connection is being aborted, e.g. should NOT perform
|
||||
* a shutdown and just close.
|
||||
**/
|
||||
typedef bool Curl_cpool_disconnect_cb(struct Curl_easy *data,
|
||||
struct connectdata *conn,
|
||||
bool aborted);
|
||||
|
||||
struct conncache {
|
||||
struct Curl_hash hash;
|
||||
struct cpool {
|
||||
/* the pooled connections, bundled per destination */
|
||||
struct Curl_hash dest2bundle;
|
||||
size_t num_conn;
|
||||
curl_off_t next_connection_id;
|
||||
curl_off_t next_easy_id;
|
||||
struct curltime last_cleanup;
|
||||
struct connshutdowns shutdowns;
|
||||
/* handle used for closing cached connections */
|
||||
struct Curl_easy *closure_handle;
|
||||
struct Curl_multi *multi; /* Optional, set if cache belongs to multi */
|
||||
struct Curl_llist shutdowns; /* The connections being shut down */
|
||||
struct Curl_easy *idata; /* internal handle used for discard */
|
||||
struct Curl_multi *multi; /* != NULL iff pool belongs to multi */
|
||||
struct Curl_share *share; /* != NULL iff pool belongs to share */
|
||||
Curl_cpool_disconnect_cb *disconnect_cb;
|
||||
BIT(locked);
|
||||
};
|
||||
|
||||
#define BUNDLE_NO_MULTIUSE -1
|
||||
#define BUNDLE_UNKNOWN 0 /* initial value */
|
||||
#define BUNDLE_MULTIPLEX 2
|
||||
|
||||
#ifdef DEBUGBUILD
|
||||
/* the debug versions of these macros make extra certain that the lock is
|
||||
never doubly locked or unlocked */
|
||||
#define CONNCACHE_LOCK(x) \
|
||||
do { \
|
||||
if((x)->share) { \
|
||||
Curl_share_lock((x), CURL_LOCK_DATA_CONNECT, \
|
||||
CURL_LOCK_ACCESS_SINGLE); \
|
||||
DEBUGASSERT(!(x)->state.conncache_lock); \
|
||||
(x)->state.conncache_lock = TRUE; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define CONNCACHE_UNLOCK(x) \
|
||||
do { \
|
||||
if((x)->share) { \
|
||||
DEBUGASSERT((x)->state.conncache_lock); \
|
||||
(x)->state.conncache_lock = FALSE; \
|
||||
Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT); \
|
||||
} \
|
||||
} while(0)
|
||||
#else
|
||||
#define CONNCACHE_LOCK(x) if((x)->share) \
|
||||
Curl_share_lock((x), CURL_LOCK_DATA_CONNECT, CURL_LOCK_ACCESS_SINGLE)
|
||||
#define CONNCACHE_UNLOCK(x) if((x)->share) \
|
||||
Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT)
|
||||
#endif
|
||||
|
||||
struct connectbundle {
|
||||
int multiuse; /* supports multi-use */
|
||||
size_t num_connections; /* Number of connections in the bundle */
|
||||
struct Curl_llist conn_list; /* The connectdata members of the bundle */
|
||||
};
|
||||
|
||||
/* Init the cache, pass multi only if cache is owned by it.
|
||||
/* Init the pool, pass multi only if pool is owned by it.
|
||||
* returns 1 on error, 0 is fine.
|
||||
*/
|
||||
int Curl_conncache_init(struct conncache *,
|
||||
struct Curl_multi *multi,
|
||||
size_t size);
|
||||
void Curl_conncache_destroy(struct conncache *connc);
|
||||
int Curl_cpool_init(struct cpool *cpool,
|
||||
Curl_cpool_disconnect_cb *disconnect_cb,
|
||||
struct Curl_multi *multi,
|
||||
struct Curl_share *share,
|
||||
size_t size);
|
||||
|
||||
/* return the correct bundle, to a host or a proxy */
|
||||
struct connectbundle *Curl_conncache_find_bundle(struct Curl_easy *data,
|
||||
struct connectdata *conn,
|
||||
struct conncache *connc);
|
||||
/* returns number of connections currently held in the connection cache */
|
||||
size_t Curl_conncache_size(struct Curl_easy *data);
|
||||
/* Destroy all connections and free all members */
|
||||
void Curl_cpool_destroy(struct cpool *connc);
|
||||
|
||||
bool Curl_conncache_return_conn(struct Curl_easy *data,
|
||||
struct connectdata *conn);
|
||||
CURLcode Curl_conncache_add_conn(struct Curl_easy *data) WARN_UNUSED_RESULT;
|
||||
void Curl_conncache_remove_conn(struct Curl_easy *data,
|
||||
struct connectdata *conn,
|
||||
bool lock);
|
||||
bool Curl_conncache_foreach(struct Curl_easy *data,
|
||||
struct conncache *connc,
|
||||
void *param,
|
||||
int (*func)(struct Curl_easy *data,
|
||||
struct connectdata *conn,
|
||||
void *param));
|
||||
|
||||
struct connectdata *
|
||||
Curl_conncache_find_first_connection(struct conncache *connc);
|
||||
|
||||
struct connectdata *
|
||||
Curl_conncache_extract_bundle(struct Curl_easy *data,
|
||||
struct connectbundle *bundle);
|
||||
struct connectdata *
|
||||
Curl_conncache_extract_oldest(struct Curl_easy *data);
|
||||
void Curl_conncache_close_all_connections(struct conncache *connc);
|
||||
void Curl_conncache_print(struct conncache *connc);
|
||||
/* Init the transfer to be used within its connection pool.
|
||||
* Assigns `data->id`. */
|
||||
void Curl_cpool_xfer_init(struct Curl_easy *data);
|
||||
|
||||
/**
|
||||
* Tear down the connection. If `aborted` is FALSE, the connection
|
||||
* will be shut down first before discarding. If the shutdown
|
||||
* is not immediately complete, the connection
|
||||
* will be placed into the cache is shutdown queue.
|
||||
* Get the connection with the given id from the transfer's pool.
|
||||
*/
|
||||
void Curl_conncache_disconnect(struct Curl_easy *data,
|
||||
struct connectdata *conn,
|
||||
bool aborted);
|
||||
struct connectdata *Curl_cpool_get_conn(struct Curl_easy *data,
|
||||
curl_off_t conn_id);
|
||||
|
||||
CURLcode Curl_cpool_add_conn(struct Curl_easy *data,
|
||||
struct connectdata *conn) WARN_UNUSED_RESULT;
|
||||
|
||||
/**
|
||||
* Add sockets and POLLIN/OUT flags for connections handled by the cache.
|
||||
* Return if the pool has reached its configured limits for adding
|
||||
* the given connection. Will try to discard the oldest, idle
|
||||
* connections to make space.
|
||||
*/
|
||||
CURLcode Curl_conncache_add_pollfds(struct conncache *connc,
|
||||
struct curl_pollfds *cpfds);
|
||||
CURLcode Curl_conncache_add_waitfds(struct conncache *connc,
|
||||
struct curl_waitfds *cwfds);
|
||||
#define CPOOL_LIMIT_OK 0
|
||||
#define CPOOL_LIMIT_DEST 1
|
||||
#define CPOOL_LIMIT_TOTAL 2
|
||||
int Curl_cpool_check_limits(struct Curl_easy *data,
|
||||
struct connectdata *conn);
|
||||
|
||||
/* Return of conn is suitable. If so, stops iteration. */
|
||||
typedef bool Curl_cpool_conn_match_cb(struct connectdata *conn,
|
||||
void *userdata);
|
||||
|
||||
/* Act on the result of the find, may override it. */
|
||||
typedef bool Curl_cpool_done_match_cb(bool result, void *userdata);
|
||||
|
||||
/**
|
||||
* Perform maintenance on connections in the cache. Specifically,
|
||||
* Find a connection in the pool matching `destination`.
|
||||
* All callbacks are invoked while the pool's lock is held.
|
||||
* @param data current transfer
|
||||
* @param destination match agaonst `conn->destination` in pool
|
||||
* @param dest_len destination length, including terminating NUL
|
||||
* @param conn_cb must be present, called for each connection in the
|
||||
* bundle until it returns TRUE
|
||||
* @param result_cb if not NULL, is called at the end with the result
|
||||
* of the `conn_cb` or FALSE if never called.
|
||||
* @return combined result of last conn_db and result_cb or FALSE if no
|
||||
connections were present.
|
||||
*/
|
||||
bool Curl_cpool_find(struct Curl_easy *data,
|
||||
const char *destination, size_t dest_len,
|
||||
Curl_cpool_conn_match_cb *conn_cb,
|
||||
Curl_cpool_done_match_cb *done_cb,
|
||||
void *userdata);
|
||||
|
||||
/*
|
||||
* A connection (already in the pool) is now idle. Do any
|
||||
* cleanups in regard to the pool's limits.
|
||||
*
|
||||
* Return TRUE if idle connection kept in pool, FALSE if closed.
|
||||
*/
|
||||
bool Curl_cpool_conn_now_idle(struct Curl_easy *data,
|
||||
struct connectdata *conn);
|
||||
|
||||
/**
|
||||
* Remove the connection from the pool and tear it down.
|
||||
* If `aborted` is FALSE, the connection will be shut down first
|
||||
* before closing and destroying it.
|
||||
* If the shutdown is not immediately complete, the connection
|
||||
* will be placed into the pool's shutdown queue.
|
||||
*/
|
||||
void Curl_cpool_disconnect(struct Curl_easy *data,
|
||||
struct connectdata *conn,
|
||||
bool aborted);
|
||||
|
||||
/**
|
||||
* This function scans the data's connection pool for half-open/dead
|
||||
* connections, closes and removes them.
|
||||
* The cleanup is done at most once per second.
|
||||
*
|
||||
* When called, this transfer has no connection attached.
|
||||
*/
|
||||
void Curl_cpool_prune_dead(struct Curl_easy *data);
|
||||
|
||||
/**
|
||||
* Perform upkeep actions on connections in the transfer's pool.
|
||||
*/
|
||||
CURLcode Curl_cpool_upkeep(void *data);
|
||||
|
||||
typedef void Curl_cpool_conn_do_cb(struct connectdata *conn,
|
||||
struct Curl_easy *data,
|
||||
void *cbdata);
|
||||
|
||||
/**
|
||||
* Invoke the callback on the pool's connection with the
|
||||
* given connection id (if it exists).
|
||||
*/
|
||||
void Curl_cpool_do_by_id(struct Curl_easy *data,
|
||||
curl_off_t conn_id,
|
||||
Curl_cpool_conn_do_cb *cb, void *cbdata);
|
||||
|
||||
/**
|
||||
* Invoked the callback for the given data + connection under the
|
||||
* connection pool's lock.
|
||||
* The callback is always invoked, even if the transfer has no connection
|
||||
* pool associated.
|
||||
*/
|
||||
void Curl_cpool_do_locked(struct Curl_easy *data,
|
||||
struct connectdata *conn,
|
||||
Curl_cpool_conn_do_cb *cb, void *cbdata);
|
||||
|
||||
/**
|
||||
* Add sockets and POLLIN/OUT flags for connections handled by the pool.
|
||||
*/
|
||||
CURLcode Curl_cpool_add_pollfds(struct cpool *connc,
|
||||
struct curl_pollfds *cpfds);
|
||||
CURLcode Curl_cpool_add_waitfds(struct cpool *connc,
|
||||
struct curl_waitfds *cwfds);
|
||||
|
||||
/**
|
||||
* Perform maintenance on connections in the pool. Specifically,
|
||||
* progress the shutdown of connections in the queue.
|
||||
*/
|
||||
void Curl_conncache_multi_perform(struct Curl_multi *multi);
|
||||
void Curl_cpool_multi_perform(struct Curl_multi *multi);
|
||||
|
||||
void Curl_cpool_multi_socket(struct Curl_multi *multi,
|
||||
curl_socket_t s, int ev_bitmask);
|
||||
|
||||
void Curl_conncache_multi_socket(struct Curl_multi *multi,
|
||||
curl_socket_t s, int ev_bitmask);
|
||||
void Curl_conncache_multi_close_all(struct Curl_multi *multi);
|
||||
|
||||
#endif /* HEADER_CURL_CONNCACHE_H */
|
||||
|
@ -312,23 +312,6 @@ bool Curl_addr2string(struct sockaddr *sa, curl_socklen_t salen,
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
struct connfind {
|
||||
curl_off_t id_tofind;
|
||||
struct connectdata *found;
|
||||
};
|
||||
|
||||
static int conn_is_conn(struct Curl_easy *data,
|
||||
struct connectdata *conn, void *param)
|
||||
{
|
||||
struct connfind *f = (struct connfind *)param;
|
||||
(void)data;
|
||||
if(conn->connection_id == f->id_tofind) {
|
||||
f->found = conn;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Used to extract socket and connectdata struct for the most recent
|
||||
* transfer on the given Curl_easy.
|
||||
@ -345,30 +328,19 @@ curl_socket_t Curl_getconnectinfo(struct Curl_easy *data,
|
||||
* - that is associated with a multi handle, and whose connection
|
||||
* was detached with CURLOPT_CONNECT_ONLY
|
||||
*/
|
||||
if((data->state.lastconnect_id != -1) && (data->multi_easy || data->multi)) {
|
||||
struct connectdata *c;
|
||||
struct connfind find;
|
||||
find.id_tofind = data->state.lastconnect_id;
|
||||
find.found = NULL;
|
||||
if(data->state.lastconnect_id != -1) {
|
||||
struct connectdata *conn;
|
||||
|
||||
Curl_conncache_foreach(data,
|
||||
data->share && (data->share->specifier
|
||||
& (1<< CURL_LOCK_DATA_CONNECT))?
|
||||
&data->share->conn_cache:
|
||||
data->multi_easy?
|
||||
&data->multi_easy->conn_cache:
|
||||
&data->multi->conn_cache, &find, conn_is_conn);
|
||||
|
||||
if(!find.found) {
|
||||
conn = Curl_cpool_get_conn(data, data->state.lastconnect_id);
|
||||
if(!conn) {
|
||||
data->state.lastconnect_id = -1;
|
||||
return CURL_SOCKET_BAD;
|
||||
}
|
||||
|
||||
c = find.found;
|
||||
if(connp)
|
||||
/* only store this if the caller cares for it */
|
||||
*connp = c;
|
||||
return c->sock[FIRSTSOCKET];
|
||||
*connp = conn;
|
||||
return conn->sock[FIRSTSOCKET];
|
||||
}
|
||||
return CURL_SOCKET_BAD;
|
||||
}
|
||||
|
68
lib/easy.c
68
lib/easy.c
@ -940,8 +940,7 @@ struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
|
||||
|
||||
Curl_dyn_init(&outcurl->state.headerb, CURL_MAX_HTTP_HEADER);
|
||||
|
||||
/* the connection cache is setup on demand */
|
||||
outcurl->state.conn_cache = NULL;
|
||||
/* the connection pool is setup on demand */
|
||||
outcurl->state.lastconnect_id = -1;
|
||||
outcurl->state.recent_conn_id = -1;
|
||||
outcurl->id = -1;
|
||||
@ -1317,54 +1316,11 @@ CURLcode curl_easy_send(struct Curl_easy *data, const void *buffer,
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* Wrapper to call functions in Curl_conncache_foreach()
|
||||
*
|
||||
* Returns always 0.
|
||||
*/
|
||||
static int conn_upkeep(struct Curl_easy *data,
|
||||
struct connectdata *conn,
|
||||
void *param)
|
||||
{
|
||||
struct curltime *now = param;
|
||||
|
||||
if(Curl_timediff(*now, conn->keepalive) <= data->set.upkeep_interval_ms)
|
||||
return 0;
|
||||
|
||||
/* briefly attach for action */
|
||||
Curl_attach_connection(data, conn);
|
||||
if(conn->handler->connection_check) {
|
||||
/* Do a protocol-specific keepalive check on the connection. */
|
||||
conn->handler->connection_check(data, conn, CONNCHECK_KEEPALIVE);
|
||||
}
|
||||
else {
|
||||
/* Do the generic action on the FIRSTSOCKET filter chain */
|
||||
Curl_conn_keep_alive(data, conn, FIRSTSOCKET);
|
||||
}
|
||||
Curl_detach_connection(data);
|
||||
|
||||
conn->keepalive = *now;
|
||||
return 0; /* continue iteration */
|
||||
}
|
||||
|
||||
static CURLcode upkeep(struct conncache *conn_cache, void *data)
|
||||
{
|
||||
struct curltime now = Curl_now();
|
||||
/* Loop over every connection and make connection alive. */
|
||||
Curl_conncache_foreach(data,
|
||||
conn_cache,
|
||||
&now,
|
||||
conn_upkeep);
|
||||
return CURLE_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Performs connection upkeep for the given session handle.
|
||||
*/
|
||||
CURLcode curl_easy_upkeep(struct Curl_easy *data)
|
||||
{
|
||||
struct conncache *conn_cache;
|
||||
|
||||
/* Verify that we got an easy handle we can work with. */
|
||||
if(!GOOD_EASY_HANDLE(data))
|
||||
return CURLE_BAD_FUNCTION_ARGUMENT;
|
||||
@ -1372,24 +1328,6 @@ CURLcode curl_easy_upkeep(struct Curl_easy *data)
|
||||
if(Curl_is_in_callback(data))
|
||||
return CURLE_RECURSIVE_API_CALL;
|
||||
|
||||
/* determine the connection cache that will next be used by the easy handle.
|
||||
if the easy handle is currently in a multi then data->state.conn_cache
|
||||
should point to the in-use cache. */
|
||||
DEBUGASSERT(!data->multi || data->state.conn_cache);
|
||||
conn_cache =
|
||||
data->state.conn_cache ?
|
||||
data->state.conn_cache :
|
||||
(data->share && (data->share->specifier & (1<< CURL_LOCK_DATA_CONNECT))) ?
|
||||
&data->share->conn_cache :
|
||||
data->multi_easy ?
|
||||
&data->multi_easy->conn_cache : NULL;
|
||||
|
||||
if(conn_cache) {
|
||||
/* Use the common function to keep connections alive. */
|
||||
return upkeep(conn_cache, data);
|
||||
}
|
||||
else {
|
||||
/* No connections, so just return success */
|
||||
return CURLE_OK;
|
||||
}
|
||||
/* Use the common function to keep connections alive. */
|
||||
return Curl_cpool_upkeep(data);
|
||||
}
|
||||
|
@ -1449,8 +1449,7 @@ CURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_done)
|
||||
|
||||
if(result) {
|
||||
Curl_detach_connection(data);
|
||||
Curl_conncache_remove_conn(data, conn, TRUE);
|
||||
Curl_disconnect(data, conn, TRUE);
|
||||
Curl_cpool_disconnect(data, conn, TRUE);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -3241,9 +3241,6 @@ CURLcode Curl_http_statusline(struct Curl_easy *data,
|
||||
else if(k->httpversion == 20 ||
|
||||
(k->upgr101 == UPGR101_H2 && k->httpcode == 101)) {
|
||||
DEBUGF(infof(data, "HTTP/2 found, allow multiplexing"));
|
||||
/* HTTP/2 cannot avoid multiplexing since it is a core functionality
|
||||
of the protocol */
|
||||
conn->bundle->multiuse = BUNDLE_MULTIPLEX;
|
||||
}
|
||||
|
||||
k->http_bodyless = k->httpcode >= 100 && k->httpcode < 200;
|
||||
@ -3393,9 +3390,6 @@ static CURLcode http_on_response(struct Curl_easy *data,
|
||||
if(conn->httpversion != 20)
|
||||
infof(data, "Lying server, not serving HTTP/2");
|
||||
}
|
||||
if(conn->httpversion < 20) {
|
||||
conn->bundle->multiuse = BUNDLE_NO_MULTIUSE;
|
||||
}
|
||||
|
||||
if(k->httpcode < 200 && last_hd) {
|
||||
/* Intermediate responses might trigger processing of more
|
||||
|
@ -2837,7 +2837,6 @@ CURLcode Curl_http2_switch(struct Curl_easy *data,
|
||||
|
||||
conn->httpversion = 20; /* we know we are on HTTP/2 now */
|
||||
conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
|
||||
conn->bundle->multiuse = BUNDLE_MULTIPLEX;
|
||||
Curl_multi_connchanged(data->multi);
|
||||
|
||||
if(cf->next) {
|
||||
@ -2861,7 +2860,6 @@ CURLcode Curl_http2_switch_at(struct Curl_cfilter *cf, struct Curl_easy *data)
|
||||
cf_h2 = cf->next;
|
||||
cf->conn->httpversion = 20; /* we know we are on HTTP/2 now */
|
||||
cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
|
||||
cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX;
|
||||
Curl_multi_connchanged(data->multi);
|
||||
|
||||
if(cf_h2->next) {
|
||||
@ -2914,7 +2912,6 @@ CURLcode Curl_http2_upgrade(struct Curl_easy *data,
|
||||
|
||||
conn->httpversion = 20; /* we know we are on HTTP/2 now */
|
||||
conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
|
||||
conn->bundle->multiuse = BUNDLE_MULTIPLEX;
|
||||
Curl_multi_connchanged(data->multi);
|
||||
|
||||
if(cf->next) {
|
||||
|
@ -254,3 +254,10 @@ struct Curl_llist_node *Curl_node_prev(struct Curl_llist_node *n)
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
struct Curl_llist *Curl_node_llist(struct Curl_llist_node *n)
|
||||
{
|
||||
DEBUGASSERT(n);
|
||||
DEBUGASSERT(!n->_list || n->_init == NODEINIT);
|
||||
return n->_list;
|
||||
}
|
||||
|
@ -83,4 +83,7 @@ struct Curl_llist_node *Curl_node_next(struct Curl_llist_node *n);
|
||||
Curl_llist_node */
|
||||
struct Curl_llist_node *Curl_node_prev(struct Curl_llist_node *n);
|
||||
|
||||
/* Curl_node_llist() return the list the node is in or NULL. */
|
||||
struct Curl_llist *Curl_node_llist(struct Curl_llist_node *n);
|
||||
|
||||
#endif /* HEADER_CURL_LLIST_H */
|
||||
|
328
lib/multi.c
328
lib/multi.c
@ -411,7 +411,8 @@ struct Curl_multi *Curl_multi_handle(size_t hashsize, /* socket hash */
|
||||
Curl_hash_init(&multi->proto_hash, 23,
|
||||
Curl_hash_str, Curl_str_key_compare, ph_freeentry);
|
||||
|
||||
if(Curl_conncache_init(&multi->conn_cache, multi, chashsize))
|
||||
if(Curl_cpool_init(&multi->cpool, Curl_on_disconnect,
|
||||
multi, NULL, chashsize))
|
||||
goto error;
|
||||
|
||||
Curl_llist_init(&multi->msglist, NULL);
|
||||
@ -443,7 +444,7 @@ error:
|
||||
sockhash_destroy(&multi->sockhash);
|
||||
Curl_hash_destroy(&multi->proto_hash);
|
||||
Curl_hash_destroy(&multi->hostcache);
|
||||
Curl_conncache_destroy(&multi->conn_cache);
|
||||
Curl_cpool_destroy(&multi->cpool);
|
||||
free(multi);
|
||||
return NULL;
|
||||
}
|
||||
@ -511,7 +512,7 @@ CURLMcode curl_multi_add_handle(struct Curl_multi *multi,
|
||||
/*
|
||||
* No failure allowed in this function beyond this point. No modification of
|
||||
* easy nor multi handle allowed before this except for potential multi's
|
||||
* connection cache growing which will not be undone in this function no
|
||||
* connection pool growing which will not be undone in this function no
|
||||
* matter what.
|
||||
*/
|
||||
if(data->set.errorbuffer)
|
||||
@ -548,13 +549,6 @@ CURLMcode curl_multi_add_handle(struct Curl_multi *multi,
|
||||
data->dns.hostcachetype = HCACHE_MULTI;
|
||||
}
|
||||
|
||||
/* Point to the shared or multi handle connection cache */
|
||||
if(data->share && (data->share->specifier & (1<< CURL_LOCK_DATA_CONNECT)))
|
||||
data->state.conn_cache = &data->share->conn_cache;
|
||||
else
|
||||
data->state.conn_cache = &multi->conn_cache;
|
||||
data->state.lastconnect_id = -1;
|
||||
|
||||
#ifdef USE_LIBPSL
|
||||
/* Do the same for PSL. */
|
||||
if(data->share && (data->share->specifier & (1 << CURL_LOCK_DATA_PSL)))
|
||||
@ -572,28 +566,12 @@ CURLMcode curl_multi_add_handle(struct Curl_multi *multi,
|
||||
/* increase the alive-counter */
|
||||
multi->num_alive++;
|
||||
|
||||
CONNCACHE_LOCK(data);
|
||||
/* The closure handle only ever has default timeouts set. To improve the
|
||||
state somewhat we clone the timeouts from each added handle so that the
|
||||
closure handle always has the same timeouts as the most recently added
|
||||
easy handle. */
|
||||
data->state.conn_cache->closure_handle->set.timeout = data->set.timeout;
|
||||
data->state.conn_cache->closure_handle->set.server_response_timeout =
|
||||
data->set.server_response_timeout;
|
||||
data->state.conn_cache->closure_handle->set.no_signal =
|
||||
data->set.no_signal;
|
||||
|
||||
/* the identifier inside the connection cache */
|
||||
data->id = data->state.conn_cache->next_easy_id++;
|
||||
if(data->state.conn_cache->next_easy_id <= 0)
|
||||
data->state.conn_cache->next_easy_id = 0;
|
||||
/* the identifier inside the multi instance */
|
||||
data->mid = multi->next_easy_mid++;
|
||||
if(multi->next_easy_mid <= 0)
|
||||
multi->next_easy_mid = 0;
|
||||
|
||||
CONNCACHE_UNLOCK(data);
|
||||
|
||||
Curl_cpool_xfer_init(data);
|
||||
multi_warn_debug(multi, data);
|
||||
|
||||
return CURLM_OK;
|
||||
@ -615,6 +593,91 @@ static void debug_print_sock_hash(void *p)
|
||||
}
|
||||
#endif
|
||||
|
||||
struct multi_done_ctx {
|
||||
BIT(premature);
|
||||
};
|
||||
|
||||
static void multi_done_locked(struct connectdata *conn,
|
||||
struct Curl_easy *data,
|
||||
void *userdata)
|
||||
{
|
||||
struct multi_done_ctx *mdctx = userdata;
|
||||
|
||||
Curl_detach_connection(data);
|
||||
|
||||
if(CONN_INUSE(conn)) {
|
||||
/* Stop if still used. */
|
||||
DEBUGF(infof(data, "Connection still in use %zu, "
|
||||
"no more multi_done now!",
|
||||
Curl_llist_count(&conn->easyq)));
|
||||
return;
|
||||
}
|
||||
|
||||
data->state.done = TRUE; /* called just now! */
|
||||
data->state.recent_conn_id = conn->connection_id;
|
||||
|
||||
if(conn->dns_entry)
|
||||
Curl_resolv_unlink(data, &conn->dns_entry); /* done with this */
|
||||
Curl_hostcache_prune(data);
|
||||
|
||||
/* if data->set.reuse_forbid is TRUE, it means the libcurl client has
|
||||
forced us to close this connection. This is ignored for requests taking
|
||||
place in a NTLM/NEGOTIATE authentication handshake
|
||||
|
||||
if conn->bits.close is TRUE, it means that the connection should be
|
||||
closed in spite of all our efforts to be nice, due to protocol
|
||||
restrictions in our or the server's end
|
||||
|
||||
if premature is TRUE, it means this connection was said to be DONE before
|
||||
the entire request operation is complete and thus we cannot know in what
|
||||
state it is for reusing, so we are forced to close it. In a perfect world
|
||||
we can add code that keep track of if we really must close it here or not,
|
||||
but currently we have no such detail knowledge.
|
||||
*/
|
||||
|
||||
if((data->set.reuse_forbid
|
||||
#if defined(USE_NTLM)
|
||||
&& !(conn->http_ntlm_state == NTLMSTATE_TYPE2 ||
|
||||
conn->proxy_ntlm_state == NTLMSTATE_TYPE2)
|
||||
#endif
|
||||
#if defined(USE_SPNEGO)
|
||||
&& !(conn->http_negotiate_state == GSS_AUTHRECV ||
|
||||
conn->proxy_negotiate_state == GSS_AUTHRECV)
|
||||
#endif
|
||||
) || conn->bits.close
|
||||
|| (mdctx->premature && !Curl_conn_is_multiplex(conn, FIRSTSOCKET))) {
|
||||
DEBUGF(infof(data, "multi_done, not reusing connection=%"
|
||||
CURL_FORMAT_CURL_OFF_T ", forbid=%d"
|
||||
", close=%d, premature=%d, conn_multiplex=%d",
|
||||
conn->connection_id, data->set.reuse_forbid,
|
||||
conn->bits.close, mdctx->premature,
|
||||
Curl_conn_is_multiplex(conn, FIRSTSOCKET)));
|
||||
connclose(conn, "disconnecting");
|
||||
Curl_cpool_disconnect(data, conn, mdctx->premature);
|
||||
}
|
||||
else {
|
||||
/* the connection is no longer in use by any transfer */
|
||||
if(Curl_cpool_conn_now_idle(data, conn)) {
|
||||
/* connection kept in the cpool */
|
||||
const char *host =
|
||||
#ifndef CURL_DISABLE_PROXY
|
||||
conn->bits.socksproxy ?
|
||||
conn->socks_proxy.host.dispname :
|
||||
conn->bits.httpproxy ? conn->http_proxy.host.dispname :
|
||||
#endif
|
||||
conn->bits.conn_to_host ? conn->conn_to_host.dispname :
|
||||
conn->host.dispname;
|
||||
data->state.lastconnect_id = conn->connection_id;
|
||||
infof(data, "Connection #%" CURL_FORMAT_CURL_OFF_T
|
||||
" to host %s left intact", conn->connection_id, host);
|
||||
}
|
||||
else {
|
||||
/* connection was removed from the cpool and destroyed. */
|
||||
data->state.lastconnect_id = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static CURLcode multi_done(struct Curl_easy *data,
|
||||
CURLcode status, /* an error if this is called
|
||||
after an error was detected */
|
||||
@ -622,6 +685,9 @@ static CURLcode multi_done(struct Curl_easy *data,
|
||||
{
|
||||
CURLcode result, r2;
|
||||
struct connectdata *conn = data->conn;
|
||||
struct multi_done_ctx mdctx;
|
||||
|
||||
memset(&mdctx, 0, sizeof(mdctx));
|
||||
|
||||
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
||||
DEBUGF(infof(data, "multi_done[%s]: status: %d prem: %d done: %d",
|
||||
@ -684,104 +750,22 @@ static CURLcode multi_done(struct Curl_easy *data,
|
||||
if(!result)
|
||||
result = Curl_req_done(&data->req, data, premature);
|
||||
|
||||
CONNCACHE_LOCK(data);
|
||||
Curl_detach_connection(data);
|
||||
if(CONN_INUSE(conn)) {
|
||||
/* Stop if still used. */
|
||||
CONNCACHE_UNLOCK(data);
|
||||
DEBUGF(infof(data, "Connection still in use %zu, "
|
||||
"no more multi_done now!",
|
||||
Curl_llist_count(&conn->easyq)));
|
||||
return CURLE_OK;
|
||||
}
|
||||
|
||||
data->state.done = TRUE; /* called just now! */
|
||||
|
||||
if(conn->dns_entry)
|
||||
Curl_resolv_unlink(data, &conn->dns_entry); /* done with this */
|
||||
Curl_hostcache_prune(data);
|
||||
|
||||
/* if data->set.reuse_forbid is TRUE, it means the libcurl client has
|
||||
forced us to close this connection. This is ignored for requests taking
|
||||
place in a NTLM/NEGOTIATE authentication handshake
|
||||
|
||||
if conn->bits.close is TRUE, it means that the connection should be
|
||||
closed in spite of all our efforts to be nice, due to protocol
|
||||
restrictions in our or the server's end
|
||||
|
||||
if premature is TRUE, it means this connection was said to be DONE before
|
||||
the entire request operation is complete and thus we cannot know in what
|
||||
state it is for reusing, so we are forced to close it. In a perfect world
|
||||
we can add code that keep track of if we really must close it here or not,
|
||||
but currently we have no such detail knowledge.
|
||||
*/
|
||||
|
||||
data->state.recent_conn_id = conn->connection_id;
|
||||
if((data->set.reuse_forbid
|
||||
#if defined(USE_NTLM)
|
||||
&& !(conn->http_ntlm_state == NTLMSTATE_TYPE2 ||
|
||||
conn->proxy_ntlm_state == NTLMSTATE_TYPE2)
|
||||
#endif
|
||||
#if defined(USE_SPNEGO)
|
||||
&& !(conn->http_negotiate_state == GSS_AUTHRECV ||
|
||||
conn->proxy_negotiate_state == GSS_AUTHRECV)
|
||||
#endif
|
||||
) || conn->bits.close
|
||||
|| (premature && !Curl_conn_is_multiplex(conn, FIRSTSOCKET))) {
|
||||
DEBUGF(infof(data, "multi_done, not reusing connection=%"
|
||||
CURL_FORMAT_CURL_OFF_T ", forbid=%d"
|
||||
", close=%d, premature=%d, conn_multiplex=%d",
|
||||
conn->connection_id,
|
||||
data->set.reuse_forbid, conn->bits.close, premature,
|
||||
Curl_conn_is_multiplex(conn, FIRSTSOCKET)));
|
||||
connclose(conn, "disconnecting");
|
||||
Curl_conncache_remove_conn(data, conn, FALSE);
|
||||
CONNCACHE_UNLOCK(data);
|
||||
Curl_disconnect(data, conn, premature);
|
||||
}
|
||||
else {
|
||||
char buffer[256];
|
||||
const char *host =
|
||||
#ifndef CURL_DISABLE_PROXY
|
||||
conn->bits.socksproxy ?
|
||||
conn->socks_proxy.host.dispname :
|
||||
conn->bits.httpproxy ? conn->http_proxy.host.dispname :
|
||||
#endif
|
||||
conn->bits.conn_to_host ? conn->conn_to_host.dispname :
|
||||
conn->host.dispname;
|
||||
/* create string before returning the connection */
|
||||
curl_off_t connection_id = conn->connection_id;
|
||||
msnprintf(buffer, sizeof(buffer),
|
||||
"Connection #%" CURL_FORMAT_CURL_OFF_T " to host %s left intact",
|
||||
connection_id, host);
|
||||
/* the connection is no longer in use by this transfer */
|
||||
CONNCACHE_UNLOCK(data);
|
||||
if(Curl_conncache_return_conn(data, conn)) {
|
||||
/* remember the most recently used connection */
|
||||
data->state.lastconnect_id = connection_id;
|
||||
data->state.recent_conn_id = connection_id;
|
||||
infof(data, "%s", buffer);
|
||||
}
|
||||
else
|
||||
data->state.lastconnect_id = -1;
|
||||
}
|
||||
/* Under the potential connection pool's share lock, decide what to
|
||||
* do with the transfer's connection. */
|
||||
mdctx.premature = premature;
|
||||
Curl_cpool_do_locked(data, data->conn, multi_done_locked, &mdctx);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int close_connect_only(struct Curl_easy *data,
|
||||
struct connectdata *conn, void *param)
|
||||
static void close_connect_only(struct connectdata *conn,
|
||||
struct Curl_easy *data,
|
||||
void *userdata)
|
||||
{
|
||||
(void)param;
|
||||
if(data->state.lastconnect_id != conn->connection_id)
|
||||
return 0;
|
||||
|
||||
if(!conn->connect_only)
|
||||
return 1;
|
||||
|
||||
connclose(conn, "Removing connect-only easy handle");
|
||||
|
||||
return 1;
|
||||
(void)userdata;
|
||||
(void)data;
|
||||
if(conn->connect_only)
|
||||
connclose(conn, "Removing connect-only easy handle");
|
||||
}
|
||||
|
||||
CURLMcode curl_multi_remove_handle(struct Curl_multi *multi,
|
||||
@ -881,15 +865,14 @@ CURLMcode curl_multi_remove_handle(struct Curl_multi *multi,
|
||||
curl_socket_t s;
|
||||
s = Curl_getconnectinfo(data, &c);
|
||||
if((s != CURL_SOCKET_BAD) && c) {
|
||||
Curl_conncache_remove_conn(data, c, TRUE);
|
||||
Curl_disconnect(data, c, TRUE);
|
||||
Curl_cpool_disconnect(data, c, TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
if(data->state.lastconnect_id != -1) {
|
||||
/* Mark any connect-only connection for closure */
|
||||
Curl_conncache_foreach(data, data->state.conn_cache,
|
||||
NULL, close_connect_only);
|
||||
Curl_cpool_do_by_id(data, data->state.lastconnect_id,
|
||||
close_connect_only, NULL);
|
||||
}
|
||||
|
||||
#ifdef USE_LIBPSL
|
||||
@ -898,10 +881,6 @@ CURLMcode curl_multi_remove_handle(struct Curl_multi *multi,
|
||||
data->psl = NULL;
|
||||
#endif
|
||||
|
||||
/* as this was using a shared connection cache we clear the pointer to that
|
||||
since we are not part of that multi handle anymore */
|
||||
data->state.conn_cache = NULL;
|
||||
|
||||
/* make sure there is no pending message in the queue sent from this easy
|
||||
handle */
|
||||
for(e = Curl_llist_head(&multi->msglist); e; e = Curl_node_next(e)) {
|
||||
@ -920,7 +899,6 @@ CURLMcode curl_multi_remove_handle(struct Curl_multi *multi,
|
||||
/* NOTE NOTE NOTE
|
||||
We do not touch the easy handle here! */
|
||||
multi->num_easy--; /* one less to care about now */
|
||||
|
||||
process_pending_handles(multi);
|
||||
|
||||
if(removed_timer) {
|
||||
@ -1223,7 +1201,7 @@ CURLMcode curl_multi_waitfds(struct Curl_multi *multi,
|
||||
}
|
||||
}
|
||||
|
||||
if(Curl_conncache_add_waitfds(&multi->conn_cache, &cwfds)) {
|
||||
if(Curl_cpool_add_waitfds(&multi->cpool, &cwfds)) {
|
||||
result = CURLM_OUT_OF_MEMORY;
|
||||
goto out;
|
||||
}
|
||||
@ -1300,7 +1278,7 @@ static CURLMcode multi_wait(struct Curl_multi *multi,
|
||||
}
|
||||
}
|
||||
|
||||
if(Curl_conncache_add_pollfds(&multi->conn_cache, &cpfds)) {
|
||||
if(Curl_cpool_add_pollfds(&multi->cpool, &cpfds)) {
|
||||
result = CURLM_OUT_OF_MEMORY;
|
||||
goto out;
|
||||
}
|
||||
@ -1945,8 +1923,14 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
|
||||
WAITDO or DO! */
|
||||
rc = CURLM_CALL_MULTI_PERFORM;
|
||||
|
||||
if(connected)
|
||||
if(connected) {
|
||||
if(!data->conn->bits.reuse &&
|
||||
Curl_conn_is_multiplex(data->conn, FIRSTSOCKET)) {
|
||||
/* new connection, can multiplex, wake pending handles */
|
||||
process_pending_handles(data->multi);
|
||||
}
|
||||
multistate(data, MSTATE_PROTOCONNECT);
|
||||
}
|
||||
else {
|
||||
multistate(data, MSTATE_CONNECTING);
|
||||
}
|
||||
@ -2055,6 +2039,11 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
|
||||
DEBUGASSERT(data->conn);
|
||||
result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &connected);
|
||||
if(connected && !result) {
|
||||
if(!data->conn->bits.reuse &&
|
||||
Curl_conn_is_multiplex(data->conn, FIRSTSOCKET)) {
|
||||
/* new connection, can multiplex, wake pending handles */
|
||||
process_pending_handles(data->multi);
|
||||
}
|
||||
rc = CURLM_CALL_MULTI_PERFORM;
|
||||
multistate(data, MSTATE_PROTOCONNECT);
|
||||
}
|
||||
@ -2511,10 +2500,6 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
|
||||
if(data->conn) {
|
||||
CURLcode res;
|
||||
|
||||
if(data->conn->bits.multiplex)
|
||||
/* Check if we can move pending requests to connection */
|
||||
process_pending_handles(multi); /* multiplexing */
|
||||
|
||||
/* post-transfer command */
|
||||
res = multi_done(data, result, FALSE);
|
||||
|
||||
@ -2589,12 +2574,7 @@ statemachine_end:
|
||||
We do not have to do this in every case block above where a
|
||||
failure is detected */
|
||||
Curl_detach_connection(data);
|
||||
|
||||
/* remove connection from cache */
|
||||
Curl_conncache_remove_conn(data, conn, TRUE);
|
||||
|
||||
/* disconnect properly */
|
||||
Curl_disconnect(data, conn, dead_connection);
|
||||
Curl_cpool_disconnect(data, conn, dead_connection);
|
||||
}
|
||||
}
|
||||
else if(data->mstate == MSTATE_CONNECT) {
|
||||
@ -2677,8 +2657,8 @@ CURLMcode curl_multi_perform(struct Curl_multi *multi, int *running_handles)
|
||||
pointer now */
|
||||
n = Curl_node_next(e);
|
||||
|
||||
if(data != multi->conn_cache.closure_handle) {
|
||||
/* connection cache handle is processed below */
|
||||
if(data != multi->cpool.idata) {
|
||||
/* connection pool handle is processed below */
|
||||
sigpipe_apply(data, &pipe_st);
|
||||
result = multi_runsingle(multi, &now, data);
|
||||
if(result)
|
||||
@ -2686,8 +2666,8 @@ CURLMcode curl_multi_perform(struct Curl_multi *multi, int *running_handles)
|
||||
}
|
||||
}
|
||||
|
||||
sigpipe_apply(multi->conn_cache.closure_handle, &pipe_st);
|
||||
Curl_conncache_multi_perform(multi);
|
||||
sigpipe_apply(multi->cpool.idata, &pipe_st);
|
||||
Curl_cpool_multi_perform(multi);
|
||||
|
||||
sigpipe_restore(&pipe_st);
|
||||
|
||||
@ -2777,8 +2757,6 @@ CURLMcode curl_multi_cleanup(struct Curl_multi *multi)
|
||||
data->dns.hostcachetype = HCACHE_NONE;
|
||||
}
|
||||
|
||||
/* Clear the pointer to the connection cache */
|
||||
data->state.conn_cache = NULL;
|
||||
data->multi = NULL; /* clear the association */
|
||||
|
||||
#ifdef USE_LIBPSL
|
||||
@ -2787,12 +2765,10 @@ CURLMcode curl_multi_cleanup(struct Curl_multi *multi)
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Close all the connections in the connection cache */
|
||||
Curl_conncache_multi_close_all(multi);
|
||||
Curl_cpool_destroy(&multi->cpool);
|
||||
|
||||
sockhash_destroy(&multi->sockhash);
|
||||
Curl_hash_destroy(&multi->proto_hash);
|
||||
Curl_conncache_destroy(&multi->conn_cache);
|
||||
Curl_hash_destroy(&multi->hostcache);
|
||||
Curl_psl_destroy(&multi->psl);
|
||||
|
||||
@ -3143,7 +3119,7 @@ struct multi_run_ctx {
|
||||
struct curltime now;
|
||||
size_t run_xfers;
|
||||
SIGPIPE_MEMBER(pipe_st);
|
||||
bool run_conn_cache;
|
||||
bool run_cpool;
|
||||
};
|
||||
|
||||
static CURLMcode multi_run_expired(struct multi_run_ctx *mrc)
|
||||
@ -3170,8 +3146,8 @@ static CURLMcode multi_run_expired(struct multi_run_ctx *mrc)
|
||||
continue;
|
||||
|
||||
(void)add_next_timeout(mrc->now, multi, data);
|
||||
if(data == multi->conn_cache.closure_handle) {
|
||||
mrc->run_conn_cache = TRUE;
|
||||
if(data == multi->cpool.idata) {
|
||||
mrc->run_cpool = TRUE;
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -3220,7 +3196,7 @@ static CURLMcode multi_socket(struct Curl_multi *multi,
|
||||
result = singlesocket(multi, Curl_node_elem(e));
|
||||
}
|
||||
}
|
||||
mrc.run_conn_cache = TRUE;
|
||||
mrc.run_cpool = TRUE;
|
||||
goto out;
|
||||
}
|
||||
|
||||
@ -3234,8 +3210,8 @@ static CURLMcode multi_socket(struct Curl_multi *multi,
|
||||
asked to get removed, so thus we better survive stray socket actions
|
||||
and just move on. */
|
||||
/* The socket might come from a connection that is being shut down
|
||||
* by the multi's conncache. */
|
||||
Curl_conncache_multi_socket(multi, s, ev_bitmask);
|
||||
* by the multi's connection pool. */
|
||||
Curl_cpool_multi_socket(multi, s, ev_bitmask);
|
||||
}
|
||||
else {
|
||||
struct Curl_hash_iterator iter;
|
||||
@ -3249,8 +3225,8 @@ static CURLMcode multi_socket(struct Curl_multi *multi,
|
||||
DEBUGASSERT(data);
|
||||
DEBUGASSERT(data->magic == CURLEASY_MAGIC_NUMBER);
|
||||
|
||||
if(data == multi->conn_cache.closure_handle)
|
||||
mrc.run_conn_cache = TRUE;
|
||||
if(data == multi->cpool.idata)
|
||||
mrc.run_cpool = TRUE;
|
||||
else {
|
||||
/* Expire with out current now, so we will get it below when
|
||||
* asking the splaytree for expired transfers. */
|
||||
@ -3275,9 +3251,9 @@ static CURLMcode multi_socket(struct Curl_multi *multi,
|
||||
}
|
||||
|
||||
out:
|
||||
if(mrc.run_conn_cache) {
|
||||
sigpipe_apply(multi->conn_cache.closure_handle, &mrc.pipe_st);
|
||||
Curl_conncache_multi_perform(multi);
|
||||
if(mrc.run_cpool) {
|
||||
sigpipe_apply(multi->cpool.idata, &mrc.pipe_st);
|
||||
Curl_cpool_multi_perform(multi);
|
||||
}
|
||||
sigpipe_restore(&mrc.pipe_st);
|
||||
|
||||
@ -3733,34 +3709,6 @@ CURLMcode curl_multi_assign(struct Curl_multi *multi, curl_socket_t s,
|
||||
return CURLM_OK;
|
||||
}
|
||||
|
||||
size_t Curl_multi_max_host_connections(struct Curl_multi *multi)
|
||||
{
|
||||
return multi ? (size_t)multi->max_host_connections : 0;
|
||||
}
|
||||
|
||||
size_t Curl_multi_max_total_connections(struct Curl_multi *multi)
|
||||
{
|
||||
return multi ? (size_t)multi->max_total_connections : 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* When information about a connection has appeared, call this!
|
||||
*/
|
||||
|
||||
void Curl_multiuse_state(struct Curl_easy *data,
|
||||
int bundlestate) /* use BUNDLE_* defines */
|
||||
{
|
||||
struct connectdata *conn;
|
||||
DEBUGASSERT(data);
|
||||
DEBUGASSERT(data->multi);
|
||||
conn = data->conn;
|
||||
DEBUGASSERT(conn);
|
||||
DEBUGASSERT(conn->bundle);
|
||||
|
||||
conn->bundle->multiuse = bundlestate;
|
||||
process_pending_handles(data->multi);
|
||||
}
|
||||
|
||||
static void move_pending_to_connect(struct Curl_multi *multi,
|
||||
struct Curl_easy *data)
|
||||
{
|
||||
|
@ -138,7 +138,7 @@ struct Curl_multi {
|
||||
struct Curl_hash proto_hash;
|
||||
|
||||
/* Shared connection cache (bundles)*/
|
||||
struct conncache conn_cache;
|
||||
struct cpool cpool;
|
||||
|
||||
long max_host_connections; /* if >0, a fixed limit of the maximum number
|
||||
of connections per host */
|
||||
|
@ -63,15 +63,6 @@ struct Curl_multi *Curl_multi_handle(size_t hashsize,
|
||||
/* mask for checking if read and/or write is set for index x */
|
||||
#define GETSOCK_MASK_RW(x) (GETSOCK_READSOCK(x)|GETSOCK_WRITESOCK(x))
|
||||
|
||||
/* Return the value of the CURLMOPT_MAX_HOST_CONNECTIONS option */
|
||||
size_t Curl_multi_max_host_connections(struct Curl_multi *multi);
|
||||
|
||||
/* Return the value of the CURLMOPT_MAX_TOTAL_CONNECTIONS option */
|
||||
size_t Curl_multi_max_total_connections(struct Curl_multi *multi);
|
||||
|
||||
void Curl_multiuse_state(struct Curl_easy *data,
|
||||
int bundlestate); /* use BUNDLE_* defines */
|
||||
|
||||
/*
|
||||
* Curl_multi_closed()
|
||||
*
|
||||
|
12
lib/share.c
12
lib/share.c
@ -31,6 +31,7 @@
|
||||
#include "psl.h"
|
||||
#include "vtls/vtls.h"
|
||||
#include "hsts.h"
|
||||
#include "url.h"
|
||||
|
||||
/* The last 3 #include files should be in this order */
|
||||
#include "curl_printf.h"
|
||||
@ -120,10 +121,11 @@ curl_share_setopt(struct Curl_share *share, CURLSHoption option, ...)
|
||||
break;
|
||||
|
||||
case CURL_LOCK_DATA_CONNECT:
|
||||
if(!share->conn_cache.hash.table) {
|
||||
if(Curl_conncache_init(&share->conn_cache, NULL, 103)) {
|
||||
/* It is safe to set this option several times on a share. */
|
||||
if(!share->cpool.idata) {
|
||||
if(Curl_cpool_init(&share->cpool, Curl_on_disconnect,
|
||||
NULL, share, 103))
|
||||
res = CURLSHE_NOMEM;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@ -228,9 +230,7 @@ curl_share_cleanup(struct Curl_share *share)
|
||||
}
|
||||
|
||||
if(share->specifier & (1 << CURL_LOCK_DATA_CONNECT)) {
|
||||
/* avoid the hash if it was never initialized */
|
||||
Curl_conncache_close_all_connections(&share->conn_cache);
|
||||
Curl_conncache_destroy(&share->conn_cache);
|
||||
Curl_cpool_destroy(&share->cpool);
|
||||
}
|
||||
Curl_hash_destroy(&share->hostcache);
|
||||
|
||||
|
@ -34,6 +34,9 @@
|
||||
#define CURL_GOOD_SHARE 0x7e117a1e
|
||||
#define GOOD_SHARE_HANDLE(x) ((x) && (x)->magic == CURL_GOOD_SHARE)
|
||||
|
||||
#define CURL_SHARE_KEEP_CONNECT(s) \
|
||||
((s) && ((s)->specifier & (1<< CURL_LOCK_DATA_CONNECT)))
|
||||
|
||||
/* this struct is libcurl-private, do not export details */
|
||||
struct Curl_share {
|
||||
unsigned int magic; /* CURL_GOOD_SHARE */
|
||||
@ -43,7 +46,7 @@ struct Curl_share {
|
||||
curl_lock_function lockfunc;
|
||||
curl_unlock_function unlockfunc;
|
||||
void *clientdata;
|
||||
struct conncache conn_cache;
|
||||
struct cpool cpool;
|
||||
struct Curl_hash hostcache;
|
||||
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
|
||||
struct CookieInfo *cookies;
|
||||
|
19
lib/url.h
19
lib/url.h
@ -37,8 +37,8 @@ void Curl_freeset(struct Curl_easy *data);
|
||||
CURLcode Curl_uc_to_curlcode(CURLUcode uc);
|
||||
CURLcode Curl_close(struct Curl_easy **datap); /* opposite of curl_open() */
|
||||
CURLcode Curl_connect(struct Curl_easy *, bool *async, bool *protocol_connect);
|
||||
void Curl_disconnect(struct Curl_easy *data,
|
||||
struct connectdata *, bool aborted);
|
||||
bool Curl_on_disconnect(struct Curl_easy *data,
|
||||
struct connectdata *, bool aborted);
|
||||
CURLcode Curl_setup_conn(struct Curl_easy *data,
|
||||
bool *protocol_done);
|
||||
void Curl_conn_free(struct Curl_easy *data, struct connectdata *conn);
|
||||
@ -65,6 +65,21 @@ void Curl_verboseconnect(struct Curl_easy *data, struct connectdata *conn,
|
||||
int sockindex);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Return TRUE iff the given connection is considered dead.
|
||||
* @param nowp NULL or pointer to time being checked against.
|
||||
*/
|
||||
bool Curl_conn_seems_dead(struct connectdata *conn,
|
||||
struct Curl_easy *data,
|
||||
struct curltime *nowp);
|
||||
|
||||
/**
|
||||
* Perform upkeep operations on the connection.
|
||||
*/
|
||||
CURLcode Curl_conn_upkeep(struct Curl_easy *data,
|
||||
struct connectdata *conn,
|
||||
struct curltime *now);
|
||||
|
||||
#if defined(USE_HTTP2) || defined(USE_HTTP3)
|
||||
void Curl_data_priority_clear_state(struct Curl_easy *data);
|
||||
#else
|
||||
|
@ -555,6 +555,7 @@ struct ConnectBits {
|
||||
BIT(aborted); /* connection was aborted, e.g. in unclean state */
|
||||
BIT(shutdown_handler); /* connection shutdown: handler shut down */
|
||||
BIT(shutdown_filters); /* connection shutdown: filters shut down */
|
||||
BIT(in_cpool); /* connection is kept in a connection pool */
|
||||
};
|
||||
|
||||
struct hostname {
|
||||
@ -802,12 +803,12 @@ struct ldapconninfo;
|
||||
* unique for an entire connection.
|
||||
*/
|
||||
struct connectdata {
|
||||
struct Curl_llist_node bundle_node; /* conncache */
|
||||
struct Curl_llist_node cpool_node; /* conncache lists */
|
||||
|
||||
curl_closesocket_callback fclosesocket; /* function closing the socket(s) */
|
||||
void *closesocket_client;
|
||||
|
||||
/* This is used by the connection cache logic. If this returns TRUE, this
|
||||
/* This is used by the connection pool logic. If this returns TRUE, this
|
||||
handle is still used by one or more easy handles and can only used by any
|
||||
other easy handle without careful consideration (== only for
|
||||
multiplexing) and it cannot be used by another multi handle! */
|
||||
@ -816,6 +817,8 @@ struct connectdata {
|
||||
/**** Fields set when inited and not modified again */
|
||||
curl_off_t connection_id; /* Contains a unique number to make it easier to
|
||||
track the connections in the log output */
|
||||
char *destination; /* string carrying normalized hostname+port+scope */
|
||||
size_t destination_len; /* strlen(destination) + 1 */
|
||||
|
||||
/* 'dns_entry' is the particular host we use. This points to an entry in the
|
||||
DNS cache and it will not get pruned while locked. It gets unlocked in
|
||||
@ -851,7 +854,7 @@ struct connectdata {
|
||||
char *oauth_bearer; /* OAUTH2 bearer, allocated */
|
||||
struct curltime now; /* "current" time */
|
||||
struct curltime created; /* creation time */
|
||||
struct curltime lastused; /* when returned to the connection cache */
|
||||
struct curltime lastused; /* when returned to the connection poolas idle */
|
||||
curl_socket_t sock[2]; /* two sockets, the second is used for the data
|
||||
transfer when doing FTP */
|
||||
Curl_recv *recv[2];
|
||||
@ -971,7 +974,6 @@ struct connectdata {
|
||||
unsigned int unused:1; /* avoids empty union */
|
||||
} proto;
|
||||
|
||||
struct connectbundle *bundle; /* The bundle we are member of */
|
||||
#ifdef USE_UNIX_SOCKETS
|
||||
char *unix_domain_socket;
|
||||
#endif
|
||||
@ -1050,7 +1052,7 @@ struct PureInfo {
|
||||
even when the session handle is no longer associated with a connection,
|
||||
and also allow curl_easy_reset() to clear this information from the
|
||||
session handle without disturbing information which is still alive, and
|
||||
that might be reused, in the connection cache. */
|
||||
that might be reused, in the connection pool. */
|
||||
struct ip_quadruple primary;
|
||||
int conn_remote_port; /* this is the "remote port", which is the port
|
||||
number of the used URL, independent of proxy or
|
||||
@ -1218,8 +1220,6 @@ struct urlpieces {
|
||||
};
|
||||
|
||||
struct UrlState {
|
||||
/* Points to the connection cache */
|
||||
struct conncache *conn_cache;
|
||||
/* buffers to store authentication data in, as parsed from input options */
|
||||
struct curltime keeps_speed; /* for the progress meter really */
|
||||
|
||||
@ -1369,9 +1369,6 @@ struct UrlState {
|
||||
unsigned char select_bits; /* != 0 -> bitmask of socket events for this
|
||||
transfer overriding anything the socket may
|
||||
report */
|
||||
#ifdef DEBUGBUILD
|
||||
BIT(conncache_lock);
|
||||
#endif
|
||||
/* when curl_easy_perform() is called, the multi handle is "owned" by
|
||||
the easy handle so curl_easy_cleanup() on such an easy handle will
|
||||
also close the multi handle! */
|
||||
@ -1907,13 +1904,13 @@ struct Curl_easy {
|
||||
/* First a simple identifier to easier detect if a user mix up this easy
|
||||
handle with a multi handle. Set this to CURLEASY_MAGIC_NUMBER */
|
||||
unsigned int magic;
|
||||
/* once an easy handle is tied to a connection cache
|
||||
/* once an easy handle is tied to a connection pool
|
||||
a non-negative number to distinguish this transfer from
|
||||
other using the same cache. For easier tracking
|
||||
other using the same pool. For easier tracking
|
||||
in log output.
|
||||
This may wrap around after LONG_MAX to 0 again, so it
|
||||
has no uniqueness guarantee for very large processings.
|
||||
Note: it has no uniqueness either IFF more than one connection cache
|
||||
Note: it has no uniqueness either IFF more than one connection pool
|
||||
is used by the libcurl application. */
|
||||
curl_off_t id;
|
||||
/* once an easy handle is added to a multi, either explicitly by the
|
||||
|
@ -911,7 +911,6 @@ static CURLcode cf_msh3_connect(struct Curl_cfilter *cf,
|
||||
CURL_TRC_CF(data, cf, "handshake succeeded");
|
||||
cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
|
||||
cf->conn->httpversion = 30;
|
||||
cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX;
|
||||
cf->connected = TRUE;
|
||||
cf->conn->alpn = CURL_HTTP_VERSION_3;
|
||||
*done = TRUE;
|
||||
|
@ -1628,7 +1628,6 @@ static CURLcode qng_verify_peer(struct Curl_cfilter *cf,
|
||||
|
||||
cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
|
||||
cf->conn->httpversion = 30;
|
||||
cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX;
|
||||
|
||||
return Curl_vquic_tls_verify_peer(&ctx->tls, cf, data, &ctx->peer);
|
||||
}
|
||||
|
@ -562,7 +562,6 @@ static CURLcode cf_osslq_verify_peer(struct Curl_cfilter *cf,
|
||||
|
||||
cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
|
||||
cf->conn->httpversion = 30;
|
||||
cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX;
|
||||
|
||||
return Curl_vquic_tls_verify_peer(&ctx->tls, cf, data, &ctx->peer);
|
||||
}
|
||||
|
@ -1380,7 +1380,6 @@ static CURLcode cf_quiche_verify_peer(struct Curl_cfilter *cf,
|
||||
|
||||
cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
|
||||
cf->conn->httpversion = 30;
|
||||
cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX;
|
||||
|
||||
return Curl_vquic_tls_verify_peer(&ctx->tls, cf, data, &ctx->peer);
|
||||
}
|
||||
|
@ -2108,9 +2108,6 @@ check_handshake:
|
||||
else
|
||||
infof(data, VTLS_INFOF_NO_ALPN);
|
||||
|
||||
Curl_multiuse_state(data, cf->conn->alpn == CURL_HTTP_VERSION_2 ?
|
||||
BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE);
|
||||
|
||||
/* chosenProtocol is a reference to the string within alpnArr
|
||||
and does not need to be freed separately */
|
||||
if(alpnArr)
|
||||
|
@ -2222,7 +2222,6 @@ CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf,
|
||||
const unsigned char *proto,
|
||||
size_t proto_len)
|
||||
{
|
||||
int can_multi = 0;
|
||||
unsigned char *palpn =
|
||||
#ifndef CURL_DISABLE_PROXY
|
||||
(cf->conn->bits.tunnel_proxy && Curl_ssl_cf_is_proxy(cf))?
|
||||
@ -2241,14 +2240,12 @@ CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf,
|
||||
else if(proto_len == ALPN_H2_LENGTH &&
|
||||
!memcmp(ALPN_H2, proto, ALPN_H2_LENGTH)) {
|
||||
*palpn = CURL_HTTP_VERSION_2;
|
||||
can_multi = 1;
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_HTTP3
|
||||
else if(proto_len == ALPN_H3_LENGTH &&
|
||||
!memcmp(ALPN_H3, proto, ALPN_H3_LENGTH)) {
|
||||
*palpn = CURL_HTTP_VERSION_3;
|
||||
can_multi = 1;
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
@ -2267,9 +2264,6 @@ CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf,
|
||||
}
|
||||
|
||||
out:
|
||||
if(!Curl_ssl_cf_is_proxy(cf))
|
||||
Curl_multiuse_state(data, can_multi?
|
||||
BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE);
|
||||
return CURLE_OK;
|
||||
}
|
||||
|
||||
|
@ -19,67 +19,57 @@ Content-Length: 29
|
||||
run 1: foobar and so on fun!
|
||||
</data>
|
||||
<datacheck>
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock SHARE
|
||||
<- Mutex unlock SHARE
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
run 1: foobar and so on fun!
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
-> Mutex lock SHARE
|
||||
<- Mutex unlock SHARE
|
||||
-> Mutex lock SHARE
|
||||
<- Mutex unlock SHARE
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
run 1: foobar and so on fun!
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
-> Mutex lock SHARE
|
||||
<- Mutex unlock SHARE
|
||||
-> Mutex lock SHARE
|
||||
<- Mutex unlock SHARE
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
run 1: foobar and so on fun!
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock
|
||||
<- Mutex unlock
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
-> Mutex lock SHARE
|
||||
<- Mutex unlock SHARE
|
||||
-> Mutex lock SHARE
|
||||
-> Mutex lock CONNECT
|
||||
<- Mutex unlock CONNECT
|
||||
<- Mutex unlock SHARE
|
||||
</datacheck>
|
||||
</reply>
|
||||
|
||||
|
@ -24,6 +24,18 @@
|
||||
#include "test.h"
|
||||
#include "memdebug.h"
|
||||
|
||||
static const char *ldata_names[] = {
|
||||
"NONE",
|
||||
"SHARE",
|
||||
"COOKIE",
|
||||
"DNS",
|
||||
"SESSION",
|
||||
"CONNECT",
|
||||
"PSL",
|
||||
"HSTS",
|
||||
"NULL",
|
||||
};
|
||||
|
||||
static void my_lock(CURL *handle, curl_lock_data data,
|
||||
curl_lock_access laccess, void *useptr)
|
||||
{
|
||||
@ -31,7 +43,7 @@ static void my_lock(CURL *handle, curl_lock_data data,
|
||||
(void)data;
|
||||
(void)laccess;
|
||||
(void)useptr;
|
||||
printf("-> Mutex lock\n");
|
||||
printf("-> Mutex lock %s\n", ldata_names[data]);
|
||||
}
|
||||
|
||||
static void my_unlock(CURL *handle, curl_lock_data data, void *useptr)
|
||||
@ -39,7 +51,7 @@ static void my_unlock(CURL *handle, curl_lock_data data, void *useptr)
|
||||
(void)handle;
|
||||
(void)data;
|
||||
(void)useptr;
|
||||
printf("<- Mutex unlock\n");
|
||||
printf("<- Mutex unlock %s\n", ldata_names[data]);
|
||||
}
|
||||
|
||||
/* test function */
|
||||
|
Loading…
Reference in New Issue
Block a user