#ifndef HEADER_CURL_CONNCACHE_H1#define HEADER_CURL_CONNCACHE_H2/***************************************************************************3* _ _ ____ _4* Project ___| | | | _ \| |5* / __| | | | |_) | |6* | (__| |_| | _ <| |___7* \___|\___/|_| \_\_____|8*9* Copyright (C) Daniel Stenberg, <[email protected]>, et al.10* Copyright (C) Linus Nielsen Feltzing, <[email protected]>11*12* This software is licensed as described in the file COPYING, which13* you should have received as part of this distribution. The terms14* are also available at https://curl.se/docs/copyright.html.15*16* You may opt to use, copy, modify, merge, publish, distribute and/or sell17* copies of the Software, and permit persons to whom the Software is18* furnished to do so, under the terms of the COPYING file.19*20* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY21* KIND, either express or implied.22*23* SPDX-License-Identifier: curl24*25***************************************************************************/26#include "curlx/timeval.h"2728struct connectdata;29struct Curl_easy;30struct curl_pollfds;31struct Curl_waitfds;32struct Curl_multi;33struct Curl_share;3435/**36* Terminate the connection, e.g. close and destroy.37* If the connection is in a cpool, remove it.38* If a `cshutdn` is available (e.g. data has a multi handle),39* pass the connection to that for controlled shutdown.40* Otherwise terminate it right away.41* Takes ownership of `conn`.42* `data` should not be attached to a connection.43*/44void Curl_conn_terminate(struct Curl_easy *data,45struct connectdata *conn,46bool aborted);4748struct cpool {49/* the pooled connections, bundled per destination */50struct Curl_hash dest2bundle;51size_t num_conn;52curl_off_t next_connection_id;53curl_off_t next_easy_id;54struct curltime last_cleanup;55struct Curl_easy *idata; /* internal handle for maintenance */56struct Curl_share *share; /* != NULL if pool belongs to share */57BIT(locked);58BIT(initialised);59};6061/* Init the pool, pass multi only if pool is owned by it.62* Cannot fail.63*/64void Curl_cpool_init(struct cpool *cpool,65struct Curl_easy *idata,66struct Curl_share *share,67size_t size);6869/* Destroy all connections and free all members */70void Curl_cpool_destroy(struct cpool *connc);7172/* Init the transfer to be used within its connection pool.73* Assigns `data->id`. */74void Curl_cpool_xfer_init(struct Curl_easy *data);7576/* Get the connection with the given id from `data`'s conn pool. */77struct connectdata *Curl_cpool_get_conn(struct Curl_easy *data,78curl_off_t conn_id);7980/* Add the connection to the pool. */81CURLcode Curl_cpool_add(struct Curl_easy *data,82struct connectdata *conn) WARN_UNUSED_RESULT;8384/**85* Return if the pool has reached its configured limits for adding86* the given connection. Will try to discard the oldest, idle87* connections to make space.88*/89#define CPOOL_LIMIT_OK 090#define CPOOL_LIMIT_DEST 191#define CPOOL_LIMIT_TOTAL 292int Curl_cpool_check_limits(struct Curl_easy *data,93struct connectdata *conn);9495/* Return of conn is suitable. If so, stops iteration. */96typedef bool Curl_cpool_conn_match_cb(struct connectdata *conn,97void *userdata);9899/* Act on the result of the find, may override it. */100typedef bool Curl_cpool_done_match_cb(bool result, void *userdata);101102/**103* Find a connection in the pool matching `destination`.104* All callbacks are invoked while the pool's lock is held.105* @param data current transfer106* @param destination match against `conn->destination` in pool107* @param conn_cb must be present, called for each connection in the108* bundle until it returns TRUE109* @return combined result of last conn_db and result_cb or FALSE if no110connections were present.111*/112bool Curl_cpool_find(struct Curl_easy *data,113const char *destination,114Curl_cpool_conn_match_cb *conn_cb,115Curl_cpool_done_match_cb *done_cb,116void *userdata);117118/*119* A connection (already in the pool) is now idle. Do any120* cleanups in regard to the pool's limits.121*122* Return TRUE if idle connection kept in pool, FALSE if closed.123*/124bool Curl_cpool_conn_now_idle(struct Curl_easy *data,125struct connectdata *conn);126127/**128* This function scans the data's connection pool for half-open/dead129* connections, closes and removes them.130* The cleanup is done at most once per second.131*132* When called, this transfer has no connection attached.133*/134void Curl_cpool_prune_dead(struct Curl_easy *data);135136/**137* Perform upkeep actions on connections in the transfer's pool.138*/139CURLcode Curl_cpool_upkeep(struct Curl_easy *data);140141typedef void Curl_cpool_conn_do_cb(struct connectdata *conn,142struct Curl_easy *data,143void *cbdata);144145/**146* Invoke the callback on the pool's connection with the147* given connection id (if it exists).148*/149void Curl_cpool_do_by_id(struct Curl_easy *data,150curl_off_t conn_id,151Curl_cpool_conn_do_cb *cb, void *cbdata);152153/**154* Invoked the callback for the given data + connection under the155* connection pool's lock.156* The callback is always invoked, even if the transfer has no connection157* pool associated.158*/159void Curl_cpool_do_locked(struct Curl_easy *data,160struct connectdata *conn,161Curl_cpool_conn_do_cb *cb, void *cbdata);162163/* Close all unused connections, prevent reuse of existing ones. */164void Curl_cpool_nw_changed(struct Curl_easy *data);165166#endif /* HEADER_CURL_CONNCACHE_H */167168169