#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***************************************************************************/2627#include <curl/curl.h>28#include "curlx/timeval.h"2930struct connectdata;31struct Curl_easy;32struct curl_pollfds;33struct Curl_waitfds;34struct Curl_multi;35struct Curl_share;3637/**38* Terminate the connection, e.g. close and destroy.39* If the connection is in a cpool, remove it.40* If a `cshutdn` is available (e.g. data has a multi handle),41* pass the connection to that for controlled shutdown.42* Otherwise terminate it right away.43* Takes ownership of `conn`.44* `data` should not be attached to a connection.45*/46void Curl_conn_terminate(struct Curl_easy *data,47struct connectdata *conn,48bool aborted);4950struct cpool {51/* the pooled connections, bundled per destination */52struct Curl_hash dest2bundle;53size_t num_conn;54curl_off_t next_connection_id;55curl_off_t next_easy_id;56struct curltime last_cleanup;57struct Curl_easy *idata; /* internal handle for maintenance */58struct Curl_share *share; /* != NULL if pool belongs to share */59BIT(locked);60BIT(initialised);61};6263/* Init the pool, pass multi only if pool is owned by it.64* Cannot fail.65*/66void Curl_cpool_init(struct cpool *cpool,67struct Curl_easy *idata,68struct Curl_share *share,69size_t size);7071/* Destroy all connections and free all members */72void Curl_cpool_destroy(struct cpool *connc);7374/* Init the transfer to be used within its connection pool.75* Assigns `data->id`. */76void Curl_cpool_xfer_init(struct Curl_easy *data);7778/* Get the connection with the given id from `data`'s conn pool. */79struct connectdata *Curl_cpool_get_conn(struct Curl_easy *data,80curl_off_t conn_id);8182/* Add the connection to the pool. */83CURLcode Curl_cpool_add(struct Curl_easy *data,84struct connectdata *conn) WARN_UNUSED_RESULT;8586/**87* Return if the pool has reached its configured limits for adding88* the given connection. Will try to discard the oldest, idle89* connections to make space.90*/91#define CPOOL_LIMIT_OK 092#define CPOOL_LIMIT_DEST 193#define CPOOL_LIMIT_TOTAL 294int Curl_cpool_check_limits(struct Curl_easy *data,95struct connectdata *conn);9697/* Return of conn is suitable. If so, stops iteration. */98typedef bool Curl_cpool_conn_match_cb(struct connectdata *conn,99void *userdata);100101/* Act on the result of the find, may override it. */102typedef bool Curl_cpool_done_match_cb(bool result, void *userdata);103104/**105* Find a connection in the pool matching `destination`.106* All callbacks are invoked while the pool's lock is held.107* @param data current transfer108* @param destination match agaonst `conn->destination` in pool109* @param conn_cb must be present, called for each connection in the110* bundle until it returns TRUE111* @return combined result of last conn_db and result_cb or FALSE if no112connections were present.113*/114bool Curl_cpool_find(struct Curl_easy *data,115const char *destination,116Curl_cpool_conn_match_cb *conn_cb,117Curl_cpool_done_match_cb *done_cb,118void *userdata);119120/*121* A connection (already in the pool) is now idle. Do any122* cleanups in regard to the pool's limits.123*124* Return TRUE if idle connection kept in pool, FALSE if closed.125*/126bool Curl_cpool_conn_now_idle(struct Curl_easy *data,127struct connectdata *conn);128129/**130* This function scans the data's connection pool for half-open/dead131* connections, closes and removes them.132* The cleanup is done at most once per second.133*134* When called, this transfer has no connection attached.135*/136void Curl_cpool_prune_dead(struct Curl_easy *data);137138/**139* Perform upkeep actions on connections in the transfer's pool.140*/141CURLcode Curl_cpool_upkeep(void *data);142143typedef void Curl_cpool_conn_do_cb(struct connectdata *conn,144struct Curl_easy *data,145void *cbdata);146147/**148* Invoke the callback on the pool's connection with the149* given connection id (if it exists).150*/151void Curl_cpool_do_by_id(struct Curl_easy *data,152curl_off_t conn_id,153Curl_cpool_conn_do_cb *cb, void *cbdata);154155/**156* Invoked the callback for the given data + connection under the157* connection pool's lock.158* The callback is always invoked, even if the transfer has no connection159* pool associated.160*/161void Curl_cpool_do_locked(struct Curl_easy *data,162struct connectdata *conn,163Curl_cpool_conn_do_cb *cb, void *cbdata);164165#endif /* HEADER_CURL_CONNCACHE_H */166167168