xref: /aosp_15_r20/external/curl/lib/conncache.h (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker #ifndef HEADER_CURL_CONNCACHE_H
2*6236dae4SAndroid Build Coastguard Worker #define HEADER_CURL_CONNCACHE_H
3*6236dae4SAndroid Build Coastguard Worker /***************************************************************************
4*6236dae4SAndroid Build Coastguard Worker  *                                  _   _ ____  _
5*6236dae4SAndroid Build Coastguard Worker  *  Project                     ___| | | |  _ \| |
6*6236dae4SAndroid Build Coastguard Worker  *                             / __| | | | |_) | |
7*6236dae4SAndroid Build Coastguard Worker  *                            | (__| |_| |  _ <| |___
8*6236dae4SAndroid Build Coastguard Worker  *                             \___|\___/|_| \_\_____|
9*6236dae4SAndroid Build Coastguard Worker  *
10*6236dae4SAndroid Build Coastguard Worker  * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
11*6236dae4SAndroid Build Coastguard Worker  * Copyright (C) Linus Nielsen Feltzing, <[email protected]>
12*6236dae4SAndroid Build Coastguard Worker  *
13*6236dae4SAndroid Build Coastguard Worker  * This software is licensed as described in the file COPYING, which
14*6236dae4SAndroid Build Coastguard Worker  * you should have received as part of this distribution. The terms
15*6236dae4SAndroid Build Coastguard Worker  * are also available at https://curl.se/docs/copyright.html.
16*6236dae4SAndroid Build Coastguard Worker  *
17*6236dae4SAndroid Build Coastguard Worker  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
18*6236dae4SAndroid Build Coastguard Worker  * copies of the Software, and permit persons to whom the Software is
19*6236dae4SAndroid Build Coastguard Worker  * furnished to do so, under the terms of the COPYING file.
20*6236dae4SAndroid Build Coastguard Worker  *
21*6236dae4SAndroid Build Coastguard Worker  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22*6236dae4SAndroid Build Coastguard Worker  * KIND, either express or implied.
23*6236dae4SAndroid Build Coastguard Worker  *
24*6236dae4SAndroid Build Coastguard Worker  * SPDX-License-Identifier: curl
25*6236dae4SAndroid Build Coastguard Worker  *
26*6236dae4SAndroid Build Coastguard Worker  ***************************************************************************/
27*6236dae4SAndroid Build Coastguard Worker 
28*6236dae4SAndroid Build Coastguard Worker #include <curl/curl.h>
29*6236dae4SAndroid Build Coastguard Worker #include "timeval.h"
30*6236dae4SAndroid Build Coastguard Worker 
31*6236dae4SAndroid Build Coastguard Worker struct connectdata;
32*6236dae4SAndroid Build Coastguard Worker struct Curl_easy;
33*6236dae4SAndroid Build Coastguard Worker struct curl_pollfds;
34*6236dae4SAndroid Build Coastguard Worker struct curl_waitfds;
35*6236dae4SAndroid Build Coastguard Worker struct Curl_multi;
36*6236dae4SAndroid Build Coastguard Worker struct Curl_share;
37*6236dae4SAndroid Build Coastguard Worker 
38*6236dae4SAndroid Build Coastguard Worker /**
39*6236dae4SAndroid Build Coastguard Worker  * Callback invoked when disconnecting connections.
40*6236dae4SAndroid Build Coastguard Worker  * @param data    transfer last handling the connection, not attached
41*6236dae4SAndroid Build Coastguard Worker  * @param conn    the connection to discard
42*6236dae4SAndroid Build Coastguard Worker  * @param aborted if the connection is being aborted
43*6236dae4SAndroid Build Coastguard Worker  * @return if the connection is being aborted, e.g. should NOT perform
44*6236dae4SAndroid Build Coastguard Worker  *         a shutdown and just close.
45*6236dae4SAndroid Build Coastguard Worker  **/
46*6236dae4SAndroid Build Coastguard Worker typedef bool Curl_cpool_disconnect_cb(struct Curl_easy *data,
47*6236dae4SAndroid Build Coastguard Worker                                       struct connectdata *conn,
48*6236dae4SAndroid Build Coastguard Worker                                       bool aborted);
49*6236dae4SAndroid Build Coastguard Worker 
50*6236dae4SAndroid Build Coastguard Worker struct cpool {
51*6236dae4SAndroid Build Coastguard Worker    /* the pooled connections, bundled per destination */
52*6236dae4SAndroid Build Coastguard Worker   struct Curl_hash dest2bundle;
53*6236dae4SAndroid Build Coastguard Worker   size_t num_conn;
54*6236dae4SAndroid Build Coastguard Worker   curl_off_t next_connection_id;
55*6236dae4SAndroid Build Coastguard Worker   curl_off_t next_easy_id;
56*6236dae4SAndroid Build Coastguard Worker   struct curltime last_cleanup;
57*6236dae4SAndroid Build Coastguard Worker   struct Curl_llist shutdowns;  /* The connections being shut down */
58*6236dae4SAndroid Build Coastguard Worker   struct Curl_easy *idata; /* internal handle used for discard */
59*6236dae4SAndroid Build Coastguard Worker   struct Curl_multi *multi; /* != NULL iff pool belongs to multi */
60*6236dae4SAndroid Build Coastguard Worker   struct Curl_share *share; /* != NULL iff pool belongs to share */
61*6236dae4SAndroid Build Coastguard Worker   Curl_cpool_disconnect_cb *disconnect_cb;
62*6236dae4SAndroid Build Coastguard Worker   BIT(locked);
63*6236dae4SAndroid Build Coastguard Worker };
64*6236dae4SAndroid Build Coastguard Worker 
65*6236dae4SAndroid Build Coastguard Worker /* Init the pool, pass multi only if pool is owned by it.
66*6236dae4SAndroid Build Coastguard Worker  * returns 1 on error, 0 is fine.
67*6236dae4SAndroid Build Coastguard Worker  */
68*6236dae4SAndroid Build Coastguard Worker int Curl_cpool_init(struct cpool *cpool,
69*6236dae4SAndroid Build Coastguard Worker                     Curl_cpool_disconnect_cb *disconnect_cb,
70*6236dae4SAndroid Build Coastguard Worker                     struct Curl_multi *multi,
71*6236dae4SAndroid Build Coastguard Worker                     struct Curl_share *share,
72*6236dae4SAndroid Build Coastguard Worker                     size_t size);
73*6236dae4SAndroid Build Coastguard Worker 
74*6236dae4SAndroid Build Coastguard Worker /* Destroy all connections and free all members */
75*6236dae4SAndroid Build Coastguard Worker void Curl_cpool_destroy(struct cpool *connc);
76*6236dae4SAndroid Build Coastguard Worker 
77*6236dae4SAndroid Build Coastguard Worker /* Init the transfer to be used within its connection pool.
78*6236dae4SAndroid Build Coastguard Worker  * Assigns `data->id`. */
79*6236dae4SAndroid Build Coastguard Worker void Curl_cpool_xfer_init(struct Curl_easy *data);
80*6236dae4SAndroid Build Coastguard Worker 
81*6236dae4SAndroid Build Coastguard Worker /**
82*6236dae4SAndroid Build Coastguard Worker  * Get the connection with the given id from the transfer's pool.
83*6236dae4SAndroid Build Coastguard Worker  */
84*6236dae4SAndroid Build Coastguard Worker struct connectdata *Curl_cpool_get_conn(struct Curl_easy *data,
85*6236dae4SAndroid Build Coastguard Worker                                         curl_off_t conn_id);
86*6236dae4SAndroid Build Coastguard Worker 
87*6236dae4SAndroid Build Coastguard Worker CURLcode Curl_cpool_add_conn(struct Curl_easy *data,
88*6236dae4SAndroid Build Coastguard Worker                              struct connectdata *conn) WARN_UNUSED_RESULT;
89*6236dae4SAndroid Build Coastguard Worker 
90*6236dae4SAndroid Build Coastguard Worker /**
91*6236dae4SAndroid Build Coastguard Worker  * Return if the pool has reached its configured limits for adding
92*6236dae4SAndroid Build Coastguard Worker  * the given connection. Will try to discard the oldest, idle
93*6236dae4SAndroid Build Coastguard Worker  * connections to make space.
94*6236dae4SAndroid Build Coastguard Worker  */
95*6236dae4SAndroid Build Coastguard Worker #define CPOOL_LIMIT_OK     0
96*6236dae4SAndroid Build Coastguard Worker #define CPOOL_LIMIT_DEST   1
97*6236dae4SAndroid Build Coastguard Worker #define CPOOL_LIMIT_TOTAL  2
98*6236dae4SAndroid Build Coastguard Worker int Curl_cpool_check_limits(struct Curl_easy *data,
99*6236dae4SAndroid Build Coastguard Worker                             struct connectdata *conn);
100*6236dae4SAndroid Build Coastguard Worker 
101*6236dae4SAndroid Build Coastguard Worker /* Return of conn is suitable. If so, stops iteration. */
102*6236dae4SAndroid Build Coastguard Worker typedef bool Curl_cpool_conn_match_cb(struct connectdata *conn,
103*6236dae4SAndroid Build Coastguard Worker                                       void *userdata);
104*6236dae4SAndroid Build Coastguard Worker 
105*6236dae4SAndroid Build Coastguard Worker /* Act on the result of the find, may override it. */
106*6236dae4SAndroid Build Coastguard Worker typedef bool Curl_cpool_done_match_cb(bool result, void *userdata);
107*6236dae4SAndroid Build Coastguard Worker 
108*6236dae4SAndroid Build Coastguard Worker /**
109*6236dae4SAndroid Build Coastguard Worker  * Find a connection in the pool matching `destination`.
110*6236dae4SAndroid Build Coastguard Worker  * All callbacks are invoked while the pool's lock is held.
111*6236dae4SAndroid Build Coastguard Worker  * @param data        current transfer
112*6236dae4SAndroid Build Coastguard Worker  * @param destination match agaonst `conn->destination` in pool
113*6236dae4SAndroid Build Coastguard Worker  * @param dest_len    destination length, including terminating NUL
114*6236dae4SAndroid Build Coastguard Worker  * @param conn_cb     must be present, called for each connection in the
115*6236dae4SAndroid Build Coastguard Worker  *                    bundle until it returns TRUE
116*6236dae4SAndroid Build Coastguard Worker  * @param result_cb   if not NULL, is called at the end with the result
117*6236dae4SAndroid Build Coastguard Worker  *                    of the `conn_cb` or FALSE if never called.
118*6236dae4SAndroid Build Coastguard Worker  * @return combined result of last conn_db and result_cb or FALSE if no
119*6236dae4SAndroid Build Coastguard Worker                       connections were present.
120*6236dae4SAndroid Build Coastguard Worker  */
121*6236dae4SAndroid Build Coastguard Worker bool Curl_cpool_find(struct Curl_easy *data,
122*6236dae4SAndroid Build Coastguard Worker                      const char *destination, size_t dest_len,
123*6236dae4SAndroid Build Coastguard Worker                      Curl_cpool_conn_match_cb *conn_cb,
124*6236dae4SAndroid Build Coastguard Worker                      Curl_cpool_done_match_cb *done_cb,
125*6236dae4SAndroid Build Coastguard Worker                      void *userdata);
126*6236dae4SAndroid Build Coastguard Worker 
127*6236dae4SAndroid Build Coastguard Worker /*
128*6236dae4SAndroid Build Coastguard Worker  * A connection (already in the pool) is now idle. Do any
129*6236dae4SAndroid Build Coastguard Worker  * cleanups in regard to the pool's limits.
130*6236dae4SAndroid Build Coastguard Worker  *
131*6236dae4SAndroid Build Coastguard Worker  * Return TRUE if idle connection kept in pool, FALSE if closed.
132*6236dae4SAndroid Build Coastguard Worker  */
133*6236dae4SAndroid Build Coastguard Worker bool Curl_cpool_conn_now_idle(struct Curl_easy *data,
134*6236dae4SAndroid Build Coastguard Worker                               struct connectdata *conn);
135*6236dae4SAndroid Build Coastguard Worker 
136*6236dae4SAndroid Build Coastguard Worker /**
137*6236dae4SAndroid Build Coastguard Worker  * Remove the connection from the pool and tear it down.
138*6236dae4SAndroid Build Coastguard Worker  * If `aborted` is FALSE, the connection will be shut down first
139*6236dae4SAndroid Build Coastguard Worker  * before closing and destroying it.
140*6236dae4SAndroid Build Coastguard Worker  * If the shutdown is not immediately complete, the connection
141*6236dae4SAndroid Build Coastguard Worker  * will be placed into the pool's shutdown queue.
142*6236dae4SAndroid Build Coastguard Worker  */
143*6236dae4SAndroid Build Coastguard Worker void Curl_cpool_disconnect(struct Curl_easy *data,
144*6236dae4SAndroid Build Coastguard Worker                            struct connectdata *conn,
145*6236dae4SAndroid Build Coastguard Worker                            bool aborted);
146*6236dae4SAndroid Build Coastguard Worker 
147*6236dae4SAndroid Build Coastguard Worker /**
148*6236dae4SAndroid Build Coastguard Worker  * This function scans the data's connection pool for half-open/dead
149*6236dae4SAndroid Build Coastguard Worker  * connections, closes and removes them.
150*6236dae4SAndroid Build Coastguard Worker  * The cleanup is done at most once per second.
151*6236dae4SAndroid Build Coastguard Worker  *
152*6236dae4SAndroid Build Coastguard Worker  * When called, this transfer has no connection attached.
153*6236dae4SAndroid Build Coastguard Worker  */
154*6236dae4SAndroid Build Coastguard Worker void Curl_cpool_prune_dead(struct Curl_easy *data);
155*6236dae4SAndroid Build Coastguard Worker 
156*6236dae4SAndroid Build Coastguard Worker /**
157*6236dae4SAndroid Build Coastguard Worker  * Perform upkeep actions on connections in the transfer's pool.
158*6236dae4SAndroid Build Coastguard Worker  */
159*6236dae4SAndroid Build Coastguard Worker CURLcode Curl_cpool_upkeep(void *data);
160*6236dae4SAndroid Build Coastguard Worker 
161*6236dae4SAndroid Build Coastguard Worker typedef void Curl_cpool_conn_do_cb(struct connectdata *conn,
162*6236dae4SAndroid Build Coastguard Worker                                    struct Curl_easy *data,
163*6236dae4SAndroid Build Coastguard Worker                                    void *cbdata);
164*6236dae4SAndroid Build Coastguard Worker 
165*6236dae4SAndroid Build Coastguard Worker /**
166*6236dae4SAndroid Build Coastguard Worker  * Invoke the callback on the pool's connection with the
167*6236dae4SAndroid Build Coastguard Worker  * given connection id (if it exists).
168*6236dae4SAndroid Build Coastguard Worker  */
169*6236dae4SAndroid Build Coastguard Worker void Curl_cpool_do_by_id(struct Curl_easy *data,
170*6236dae4SAndroid Build Coastguard Worker                          curl_off_t conn_id,
171*6236dae4SAndroid Build Coastguard Worker                          Curl_cpool_conn_do_cb *cb, void *cbdata);
172*6236dae4SAndroid Build Coastguard Worker 
173*6236dae4SAndroid Build Coastguard Worker /**
174*6236dae4SAndroid Build Coastguard Worker  * Invoked the callback for the given data + connection under the
175*6236dae4SAndroid Build Coastguard Worker  * connection pool's lock.
176*6236dae4SAndroid Build Coastguard Worker  * The callback is always invoked, even if the transfer has no connection
177*6236dae4SAndroid Build Coastguard Worker  * pool associated.
178*6236dae4SAndroid Build Coastguard Worker  */
179*6236dae4SAndroid Build Coastguard Worker void Curl_cpool_do_locked(struct Curl_easy *data,
180*6236dae4SAndroid Build Coastguard Worker                           struct connectdata *conn,
181*6236dae4SAndroid Build Coastguard Worker                           Curl_cpool_conn_do_cb *cb, void *cbdata);
182*6236dae4SAndroid Build Coastguard Worker 
183*6236dae4SAndroid Build Coastguard Worker /**
184*6236dae4SAndroid Build Coastguard Worker  * Add sockets and POLLIN/OUT flags for connections handled by the pool.
185*6236dae4SAndroid Build Coastguard Worker  */
186*6236dae4SAndroid Build Coastguard Worker CURLcode Curl_cpool_add_pollfds(struct cpool *connc,
187*6236dae4SAndroid Build Coastguard Worker                                 struct curl_pollfds *cpfds);
188*6236dae4SAndroid Build Coastguard Worker CURLcode Curl_cpool_add_waitfds(struct cpool *connc,
189*6236dae4SAndroid Build Coastguard Worker                                 struct curl_waitfds *cwfds);
190*6236dae4SAndroid Build Coastguard Worker 
191*6236dae4SAndroid Build Coastguard Worker /**
192*6236dae4SAndroid Build Coastguard Worker  * Perform maintenance on connections in the pool. Specifically,
193*6236dae4SAndroid Build Coastguard Worker  * progress the shutdown of connections in the queue.
194*6236dae4SAndroid Build Coastguard Worker  */
195*6236dae4SAndroid Build Coastguard Worker void Curl_cpool_multi_perform(struct Curl_multi *multi);
196*6236dae4SAndroid Build Coastguard Worker 
197*6236dae4SAndroid Build Coastguard Worker void Curl_cpool_multi_socket(struct Curl_multi *multi,
198*6236dae4SAndroid Build Coastguard Worker                              curl_socket_t s, int ev_bitmask);
199*6236dae4SAndroid Build Coastguard Worker 
200*6236dae4SAndroid Build Coastguard Worker 
201*6236dae4SAndroid Build Coastguard Worker #endif /* HEADER_CURL_CONNCACHE_H */
202