1#ifndef HEADER_CURL_CONNCACHE_H
2#define HEADER_CURL_CONNCACHE_H
3/***************************************************************************
4 * _ _ ____ _
5 * Project ___| | | | _ \| |
6 * / __| | | | |_) | |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
9 *
10 * Copyright (C) 2015 - 2022, Daniel Stenberg, <[email protected]>, et al.
11 * Copyright (C) 2012 - 2014, 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/*
29 * All accesses to struct fields and changing of data in the connection cache
30 * and connectbundles must be done with the conncache LOCKED. The cache might
31 * be shared.
32 */
33
34#include "timeval.h"
35
36struct connectdata;
37
38struct conncache {
39 struct Curl_hash hash;
40 size_t num_conn;
41 long next_connection_id;
42 struct curltime last_cleanup;
43 /* handle used for closing cached connections */
44 struct Curl_easy *closure_handle;
45};
46
47#define BUNDLE_NO_MULTIUSE -1
48#define BUNDLE_UNKNOWN 0 /* initial value */
49#define BUNDLE_MULTIPLEX 2
50
51#ifdef CURLDEBUG
52/* the debug versions of these macros make extra certain that the lock is
53 never doubly locked or unlocked */
54#define CONNCACHE_LOCK(x) \
55 do { \
56 if((x)->share) { \
57 Curl_share_lock((x), CURL_LOCK_DATA_CONNECT, \
58 CURL_LOCK_ACCESS_SINGLE); \
59 DEBUGASSERT(!(x)->state.conncache_lock); \
60 (x)->state.conncache_lock = TRUE; \
61 } \
62 } while(0)
63
64#define CONNCACHE_UNLOCK(x) \
65 do { \
66 if((x)->share) { \
67 DEBUGASSERT((x)->state.conncache_lock); \
68 (x)->state.conncache_lock = FALSE; \
69 Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT); \
70 } \
71 } while(0)
72#else
73#define CONNCACHE_LOCK(x) if((x)->share) \
74 Curl_share_lock((x), CURL_LOCK_DATA_CONNECT, CURL_LOCK_ACCESS_SINGLE)
75#define CONNCACHE_UNLOCK(x) if((x)->share) \
76 Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT)
77#endif
78
79struct connectbundle {
80 int multiuse; /* supports multi-use */
81 size_t num_connections; /* Number of connections in the bundle */
82 struct Curl_llist conn_list; /* The connectdata members of the bundle */
83};
84
85/* returns 1 on error, 0 is fine */
86int Curl_conncache_init(struct conncache *, int size);
87void Curl_conncache_destroy(struct conncache *connc);
88
89/* return the correct bundle, to a host or a proxy */
90struct connectbundle *Curl_conncache_find_bundle(struct Curl_easy *data,
91 struct connectdata *conn,
92 struct conncache *connc);
93/* returns number of connections currently held in the connection cache */
94size_t Curl_conncache_size(struct Curl_easy *data);
95
96bool Curl_conncache_return_conn(struct Curl_easy *data,
97 struct connectdata *conn);
98CURLcode Curl_conncache_add_conn(struct Curl_easy *data) WARN_UNUSED_RESULT;
99void Curl_conncache_remove_conn(struct Curl_easy *data,
100 struct connectdata *conn,
101 bool lock);
102bool Curl_conncache_foreach(struct Curl_easy *data,
103 struct conncache *connc,
104 void *param,
105 int (*func)(struct Curl_easy *data,
106 struct connectdata *conn,
107 void *param));
108
109struct connectdata *
110Curl_conncache_find_first_connection(struct conncache *connc);
111
112struct connectdata *
113Curl_conncache_extract_bundle(struct Curl_easy *data,
114 struct connectbundle *bundle);
115struct connectdata *
116Curl_conncache_extract_oldest(struct Curl_easy *data);
117void Curl_conncache_close_all_connections(struct conncache *connc);
118void Curl_conncache_print(struct conncache *connc);
119
120#endif /* HEADER_CURL_CONNCACHE_H */
121