• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*
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 <curl/curl.h>
35 #include "timeval.h"
36 
37 struct connectdata;
38 
39 struct conncache {
40   struct Curl_hash hash;
41   size_t num_conn;
42   curl_off_t next_connection_id;
43   curl_off_t next_easy_id;
44   struct curltime last_cleanup;
45   /* handle used for closing cached connections */
46   struct Curl_easy *closure_handle;
47 };
48 
49 #define BUNDLE_NO_MULTIUSE -1
50 #define BUNDLE_UNKNOWN     0  /* initial value */
51 #define BUNDLE_MULTIPLEX   2
52 
53 #ifdef CURLDEBUG
54 /* the debug versions of these macros make extra certain that the lock is
55    never doubly locked or unlocked */
56 #define CONNCACHE_LOCK(x)                                               \
57   do {                                                                  \
58     if((x)->share) {                                                    \
59       Curl_share_lock((x), CURL_LOCK_DATA_CONNECT,                      \
60                       CURL_LOCK_ACCESS_SINGLE);                         \
61       DEBUGASSERT(!(x)->state.conncache_lock);                          \
62       (x)->state.conncache_lock = TRUE;                                 \
63     }                                                                   \
64   } while(0)
65 
66 #define CONNCACHE_UNLOCK(x)                                             \
67   do {                                                                  \
68     if((x)->share) {                                                    \
69       DEBUGASSERT((x)->state.conncache_lock);                           \
70       (x)->state.conncache_lock = FALSE;                                \
71       Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT);                   \
72     }                                                                   \
73   } while(0)
74 #else
75 #define CONNCACHE_LOCK(x) if((x)->share)                                \
76     Curl_share_lock((x), CURL_LOCK_DATA_CONNECT, CURL_LOCK_ACCESS_SINGLE)
77 #define CONNCACHE_UNLOCK(x) if((x)->share)              \
78     Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT)
79 #endif
80 
81 struct connectbundle {
82   int multiuse;                 /* supports multi-use */
83   size_t num_connections;       /* Number of connections in the bundle */
84   struct Curl_llist conn_list;  /* The connectdata members of the bundle */
85 };
86 
87 /* returns 1 on error, 0 is fine */
88 int Curl_conncache_init(struct conncache *, int size);
89 void Curl_conncache_destroy(struct conncache *connc);
90 
91 /* return the correct bundle, to a host or a proxy */
92 struct connectbundle *Curl_conncache_find_bundle(struct Curl_easy *data,
93                                                  struct connectdata *conn,
94                                                  struct conncache *connc);
95 /* returns number of connections currently held in the connection cache */
96 size_t Curl_conncache_size(struct Curl_easy *data);
97 
98 bool Curl_conncache_return_conn(struct Curl_easy *data,
99                                 struct connectdata *conn);
100 CURLcode Curl_conncache_add_conn(struct Curl_easy *data) WARN_UNUSED_RESULT;
101 void Curl_conncache_remove_conn(struct Curl_easy *data,
102                                 struct connectdata *conn,
103                                 bool lock);
104 bool Curl_conncache_foreach(struct Curl_easy *data,
105                             struct conncache *connc,
106                             void *param,
107                             int (*func)(struct Curl_easy *data,
108                                         struct connectdata *conn,
109                                         void *param));
110 
111 struct connectdata *
112 Curl_conncache_find_first_connection(struct conncache *connc);
113 
114 struct connectdata *
115 Curl_conncache_extract_bundle(struct Curl_easy *data,
116                               struct connectbundle *bundle);
117 struct connectdata *
118 Curl_conncache_extract_oldest(struct Curl_easy *data);
119 void Curl_conncache_close_all_connections(struct conncache *connc);
120 void Curl_conncache_print(struct conncache *connc);
121 
122 #endif /* HEADER_CURL_CONNCACHE_H */
123