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, <daniel@haxx.se>, et al. 11 * Copyright (C) Linus Nielsen Feltzing, <linus@haxx.se> 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 "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 * Callback invoked when disconnecting connections. 40 * @param data transfer last handling the connection, not attached 41 * @param conn the connection to discard 42 * @param aborted if the connection is being aborted 43 * @return if the connection is being aborted, e.g. should NOT perform 44 * a shutdown and just close. 45 **/ 46 typedef bool Curl_cpool_disconnect_cb(struct Curl_easy *data, 47 struct connectdata *conn, 48 bool aborted); 49 50 struct cpool { 51 /* the pooled connections, bundled per destination */ 52 struct Curl_hash dest2bundle; 53 size_t num_conn; 54 curl_off_t next_connection_id; 55 curl_off_t next_easy_id; 56 struct curltime last_cleanup; 57 struct Curl_llist shutdowns; /* The connections being shut down */ 58 struct Curl_easy *idata; /* internal handle used for discard */ 59 struct Curl_multi *multi; /* != NULL iff pool belongs to multi */ 60 struct Curl_share *share; /* != NULL iff pool belongs to share */ 61 Curl_cpool_disconnect_cb *disconnect_cb; 62 BIT(locked); 63 }; 64 65 /* Init the pool, pass multi only if pool is owned by it. 66 * returns 1 on error, 0 is fine. 67 */ 68 int Curl_cpool_init(struct cpool *cpool, 69 Curl_cpool_disconnect_cb *disconnect_cb, 70 struct Curl_multi *multi, 71 struct Curl_share *share, 72 size_t size); 73 74 /* Destroy all connections and free all members */ 75 void Curl_cpool_destroy(struct cpool *connc); 76 77 /* Init the transfer to be used within its connection pool. 78 * Assigns `data->id`. */ 79 void Curl_cpool_xfer_init(struct Curl_easy *data); 80 81 /** 82 * Get the connection with the given id from the transfer's pool. 83 */ 84 struct connectdata *Curl_cpool_get_conn(struct Curl_easy *data, 85 curl_off_t conn_id); 86 87 CURLcode Curl_cpool_add_conn(struct Curl_easy *data, 88 struct connectdata *conn) WARN_UNUSED_RESULT; 89 90 /** 91 * Return if the pool has reached its configured limits for adding 92 * the given connection. Will try to discard the oldest, idle 93 * connections to make space. 94 */ 95 #define CPOOL_LIMIT_OK 0 96 #define CPOOL_LIMIT_DEST 1 97 #define CPOOL_LIMIT_TOTAL 2 98 int Curl_cpool_check_limits(struct Curl_easy *data, 99 struct connectdata *conn); 100 101 /* Return of conn is suitable. If so, stops iteration. */ 102 typedef bool Curl_cpool_conn_match_cb(struct connectdata *conn, 103 void *userdata); 104 105 /* Act on the result of the find, may override it. */ 106 typedef bool Curl_cpool_done_match_cb(bool result, void *userdata); 107 108 /** 109 * Find a connection in the pool matching `destination`. 110 * All callbacks are invoked while the pool's lock is held. 111 * @param data current transfer 112 * @param destination match agaonst `conn->destination` in pool 113 * @param dest_len destination length, including terminating NUL 114 * @param conn_cb must be present, called for each connection in the 115 * bundle until it returns TRUE 116 * @param result_cb if not NULL, is called at the end with the result 117 * of the `conn_cb` or FALSE if never called. 118 * @return combined result of last conn_db and result_cb or FALSE if no 119 connections were present. 120 */ 121 bool Curl_cpool_find(struct Curl_easy *data, 122 const char *destination, size_t dest_len, 123 Curl_cpool_conn_match_cb *conn_cb, 124 Curl_cpool_done_match_cb *done_cb, 125 void *userdata); 126 127 /* 128 * A connection (already in the pool) is now idle. Do any 129 * cleanups in regard to the pool's limits. 130 * 131 * Return TRUE if idle connection kept in pool, FALSE if closed. 132 */ 133 bool Curl_cpool_conn_now_idle(struct Curl_easy *data, 134 struct connectdata *conn); 135 136 /** 137 * Remove the connection from the pool and tear it down. 138 * If `aborted` is FALSE, the connection will be shut down first 139 * before closing and destroying it. 140 * If the shutdown is not immediately complete, the connection 141 * will be placed into the pool's shutdown queue. 142 */ 143 void Curl_cpool_disconnect(struct Curl_easy *data, 144 struct connectdata *conn, 145 bool aborted); 146 147 /** 148 * This function scans the data's connection pool for half-open/dead 149 * connections, closes and removes them. 150 * The cleanup is done at most once per second. 151 * 152 * When called, this transfer has no connection attached. 153 */ 154 void Curl_cpool_prune_dead(struct Curl_easy *data); 155 156 /** 157 * Perform upkeep actions on connections in the transfer's pool. 158 */ 159 CURLcode Curl_cpool_upkeep(void *data); 160 161 typedef void Curl_cpool_conn_do_cb(struct connectdata *conn, 162 struct Curl_easy *data, 163 void *cbdata); 164 165 /** 166 * Invoke the callback on the pool's connection with the 167 * given connection id (if it exists). 168 */ 169 void Curl_cpool_do_by_id(struct Curl_easy *data, 170 curl_off_t conn_id, 171 Curl_cpool_conn_do_cb *cb, void *cbdata); 172 173 /** 174 * Invoked the callback for the given data + connection under the 175 * connection pool's lock. 176 * The callback is always invoked, even if the transfer has no connection 177 * pool associated. 178 */ 179 void Curl_cpool_do_locked(struct Curl_easy *data, 180 struct connectdata *conn, 181 Curl_cpool_conn_do_cb *cb, void *cbdata); 182 183 /** 184 * Add sockets and POLLIN/OUT flags for connections handled by the pool. 185 */ 186 CURLcode Curl_cpool_add_pollfds(struct cpool *connc, 187 struct curl_pollfds *cpfds); 188 CURLcode Curl_cpool_add_waitfds(struct cpool *connc, 189 struct curl_waitfds *cwfds); 190 191 /** 192 * Perform maintenance on connections in the pool. Specifically, 193 * progress the shutdown of connections in the queue. 194 */ 195 void Curl_cpool_multi_perform(struct Curl_multi *multi); 196 197 void Curl_cpool_multi_socket(struct Curl_multi *multi, 198 curl_socket_t s, int ev_bitmask); 199 200 201 #endif /* HEADER_CURL_CONNCACHE_H */ 202