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 * @return combined result of last conn_db and result_cb or FALSE if no 117 connections were present. 118 */ 119 bool Curl_cpool_find(struct Curl_easy *data, 120 const char *destination, size_t dest_len, 121 Curl_cpool_conn_match_cb *conn_cb, 122 Curl_cpool_done_match_cb *done_cb, 123 void *userdata); 124 125 /* 126 * A connection (already in the pool) is now idle. Do any 127 * cleanups in regard to the pool's limits. 128 * 129 * Return TRUE if idle connection kept in pool, FALSE if closed. 130 */ 131 bool Curl_cpool_conn_now_idle(struct Curl_easy *data, 132 struct connectdata *conn); 133 134 /** 135 * Remove the connection from the pool and tear it down. 136 * If `aborted` is FALSE, the connection will be shut down first 137 * before closing and destroying it. 138 * If the shutdown is not immediately complete, the connection 139 * will be placed into the pool's shutdown queue. 140 */ 141 void Curl_cpool_disconnect(struct Curl_easy *data, 142 struct connectdata *conn, 143 bool aborted); 144 145 /** 146 * This function scans the data's connection pool for half-open/dead 147 * connections, closes and removes them. 148 * The cleanup is done at most once per second. 149 * 150 * When called, this transfer has no connection attached. 151 */ 152 void Curl_cpool_prune_dead(struct Curl_easy *data); 153 154 /** 155 * Perform upkeep actions on connections in the transfer's pool. 156 */ 157 CURLcode Curl_cpool_upkeep(void *data); 158 159 typedef void Curl_cpool_conn_do_cb(struct connectdata *conn, 160 struct Curl_easy *data, 161 void *cbdata); 162 163 /** 164 * Invoke the callback on the pool's connection with the 165 * given connection id (if it exists). 166 */ 167 void Curl_cpool_do_by_id(struct Curl_easy *data, 168 curl_off_t conn_id, 169 Curl_cpool_conn_do_cb *cb, void *cbdata); 170 171 /** 172 * Invoked the callback for the given data + connection under the 173 * connection pool's lock. 174 * The callback is always invoked, even if the transfer has no connection 175 * pool associated. 176 */ 177 void Curl_cpool_do_locked(struct Curl_easy *data, 178 struct connectdata *conn, 179 Curl_cpool_conn_do_cb *cb, void *cbdata); 180 181 /** 182 * Add sockets and POLLIN/OUT flags for connections handled by the pool. 183 */ 184 CURLcode Curl_cpool_add_pollfds(struct cpool *connc, 185 struct curl_pollfds *cpfds); 186 unsigned int Curl_cpool_add_waitfds(struct cpool *connc, 187 struct Curl_waitfds *cwfds); 188 189 void Curl_cpool_setfds(struct cpool *cpool, 190 fd_set *read_fd_set, fd_set *write_fd_set, 191 int *maxfd); 192 193 /** 194 * Perform maintenance on connections in the pool. Specifically, 195 * progress the shutdown of connections in the queue. 196 */ 197 void Curl_cpool_multi_perform(struct Curl_multi *multi); 198 199 void Curl_cpool_multi_socket(struct Curl_multi *multi, 200 curl_socket_t s, int ev_bitmask); 201 202 203 #endif /* HEADER_CURL_CONNCACHE_H */ 204