| 1 | #ifndef HEADER_CURL_CONNCACHE_H |
| 2 | #define HEADER_CURL_CONNCACHE_H |
| 3 | /*************************************************************************** |
| 4 | * _ _ ____ _ |
| 5 | * Project ___| | | | _ \| | |
| 6 | * / __| | | | |_) | | |
| 7 | * | (__| |_| | _ <| |___ |
| 8 | * \___|\___/|_| \_\_____| |
| 9 | * |
| 10 | * Copyright (C) Daniel Stenberg, <[email protected]>, et al. |
| 11 | * Copyright (C) Linus Nielsen Feltzing, <[email protected]> |
| 12 | * |
| 13 | * This software is licensed as described in the file COPYING, which |
| 14 | * you should have received as part of this distribution. The terms |
| 15 | * are also available at https://curl.se/docs/copyright.html. |
| 16 | * |
| 17 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 18 | * copies of the Software, and permit persons to whom the Software is |
| 19 | * furnished to do so, under the terms of the COPYING file. |
| 20 | * |
| 21 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 22 | * KIND, either express or implied. |
| 23 | * |
| 24 | * SPDX-License-Identifier: curl |
| 25 | * |
| 26 | ***************************************************************************/ |
| 27 | |
| 28 | #include <curl/curl.h> |
| 29 | #include "curlx/timeval.h" |
| 30 | |
| 31 | struct connectdata; |
| 32 | struct Curl_easy; |
| 33 | struct curl_pollfds; |
| 34 | struct Curl_waitfds; |
| 35 | struct Curl_multi; |
| 36 | struct Curl_share; |
| 37 | |
| 38 | /** |
| 39 | * Terminate the connection, e.g. close and destroy. |
| 40 | * If the connection is in a cpool, remove it. |
| 41 | * If a `cshutdn` is available (e.g. data has a multi handle), |
| 42 | * pass the connection to that for controlled shutdown. |
| 43 | * Otherwise terminate it right away. |
| 44 | * Takes ownership of `conn`. |
| 45 | * `data` should not be attached to a connection. |
| 46 | */ |
| 47 | void Curl_conn_terminate(struct Curl_easy *data, |
| 48 | struct connectdata *conn, |
| 49 | bool aborted); |
| 50 | |
| 51 | struct cpool { |
| 52 | /* the pooled connections, bundled per destination */ |
| 53 | struct Curl_hash dest2bundle; |
| 54 | size_t num_conn; |
| 55 | curl_off_t next_connection_id; |
| 56 | curl_off_t next_easy_id; |
| 57 | struct curltime last_cleanup; |
| 58 | struct Curl_easy *idata; /* internal handle for maintenance */ |
| 59 | struct Curl_share *share; /* != NULL if pool belongs to share */ |
| 60 | BIT(locked); |
| 61 | BIT(initialised); |
| 62 | }; |
| 63 | |
| 64 | /* Init the pool, pass multi only if pool is owned by it. |
| 65 | * Cannot fail. |
| 66 | */ |
| 67 | void Curl_cpool_init(struct cpool *cpool, |
| 68 | struct Curl_easy *idata, |
| 69 | struct Curl_share *share, |
| 70 | size_t size); |
| 71 | |
| 72 | /* Destroy all connections and free all members */ |
| 73 | void Curl_cpool_destroy(struct cpool *connc); |
| 74 | |
| 75 | /* Init the transfer to be used within its connection pool. |
| 76 | * Assigns `data->id`. */ |
| 77 | void Curl_cpool_xfer_init(struct Curl_easy *data); |
| 78 | |
| 79 | /* Get the connection with the given id from `data`'s conn pool. */ |
| 80 | struct connectdata *Curl_cpool_get_conn(struct Curl_easy *data, |
| 81 | curl_off_t conn_id); |
| 82 | |
| 83 | /* Add the connection to the pool. */ |
| 84 | CURLcode Curl_cpool_add(struct Curl_easy *data, |
| 85 | struct connectdata *conn) WARN_UNUSED_RESULT; |
| 86 | |
| 87 | /** |
| 88 | * Return if the pool has reached its configured limits for adding |
| 89 | * the given connection. Will try to discard the oldest, idle |
| 90 | * connections to make space. |
| 91 | */ |
| 92 | #define CPOOL_LIMIT_OK 0 |
| 93 | #define CPOOL_LIMIT_DEST 1 |
| 94 | #define CPOOL_LIMIT_TOTAL 2 |
| 95 | int Curl_cpool_check_limits(struct Curl_easy *data, |
| 96 | struct connectdata *conn); |
| 97 | |
| 98 | /* Return of conn is suitable. If so, stops iteration. */ |
| 99 | typedef bool Curl_cpool_conn_match_cb(struct connectdata *conn, |
| 100 | void *userdata); |
| 101 | |
| 102 | /* Act on the result of the find, may override it. */ |
| 103 | typedef bool Curl_cpool_done_match_cb(bool result, void *userdata); |
| 104 | |
| 105 | /** |
| 106 | * Find a connection in the pool matching `destination`. |
| 107 | * All callbacks are invoked while the pool's lock is held. |
| 108 | * @param data current transfer |
| 109 | * @param destination match agaonst `conn->destination` in pool |
| 110 | * @param conn_cb must be present, called for each connection in the |
| 111 | * bundle until it returns TRUE |
| 112 | * @return combined result of last conn_db and result_cb or FALSE if no |
| 113 | connections were present. |
| 114 | */ |
| 115 | bool Curl_cpool_find(struct Curl_easy *data, |
| 116 | const char *destination, |
| 117 | Curl_cpool_conn_match_cb *conn_cb, |
| 118 | Curl_cpool_done_match_cb *done_cb, |
| 119 | void *userdata); |
| 120 | |
| 121 | /* |
| 122 | * A connection (already in the pool) is now idle. Do any |
| 123 | * cleanups in regard to the pool's limits. |
| 124 | * |
| 125 | * Return TRUE if idle connection kept in pool, FALSE if closed. |
| 126 | */ |
| 127 | bool Curl_cpool_conn_now_idle(struct Curl_easy *data, |
| 128 | struct connectdata *conn); |
| 129 | |
| 130 | /** |
| 131 | * This function scans the data's connection pool for half-open/dead |
| 132 | * connections, closes and removes them. |
| 133 | * The cleanup is done at most once per second. |
| 134 | * |
| 135 | * When called, this transfer has no connection attached. |
| 136 | */ |
| 137 | void Curl_cpool_prune_dead(struct Curl_easy *data); |
| 138 | |
| 139 | /** |
| 140 | * Perform upkeep actions on connections in the transfer's pool. |
| 141 | */ |
| 142 | CURLcode Curl_cpool_upkeep(void *data); |
| 143 | |
| 144 | typedef void Curl_cpool_conn_do_cb(struct connectdata *conn, |
| 145 | struct Curl_easy *data, |
| 146 | void *cbdata); |
| 147 | |
| 148 | /** |
| 149 | * Invoke the callback on the pool's connection with the |
| 150 | * given connection id (if it exists). |
| 151 | */ |
| 152 | void Curl_cpool_do_by_id(struct Curl_easy *data, |
| 153 | curl_off_t conn_id, |
| 154 | Curl_cpool_conn_do_cb *cb, void *cbdata); |
| 155 | |
| 156 | /** |
| 157 | * Invoked the callback for the given data + connection under the |
| 158 | * connection pool's lock. |
| 159 | * The callback is always invoked, even if the transfer has no connection |
| 160 | * pool associated. |
| 161 | */ |
| 162 | void Curl_cpool_do_locked(struct Curl_easy *data, |
| 163 | struct connectdata *conn, |
| 164 | Curl_cpool_conn_do_cb *cb, void *cbdata); |
| 165 | |
| 166 | #endif /* HEADER_CURL_CONNCACHE_H */ |