Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmcurl/lib/conncache.h
5020 views
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, <[email protected]>, et al.
11
* Copyright (C) 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
#include "curlx/timeval.h"
28
29
struct connectdata;
30
struct Curl_easy;
31
struct curl_pollfds;
32
struct Curl_waitfds;
33
struct Curl_multi;
34
struct Curl_share;
35
36
/**
37
* Terminate the connection, e.g. close and destroy.
38
* If the connection is in a cpool, remove it.
39
* If a `cshutdn` is available (e.g. data has a multi handle),
40
* pass the connection to that for controlled shutdown.
41
* Otherwise terminate it right away.
42
* Takes ownership of `conn`.
43
* `data` should not be attached to a connection.
44
*/
45
void Curl_conn_terminate(struct Curl_easy *data,
46
struct connectdata *conn,
47
bool aborted);
48
49
struct cpool {
50
/* the pooled connections, bundled per destination */
51
struct Curl_hash dest2bundle;
52
size_t num_conn;
53
curl_off_t next_connection_id;
54
curl_off_t next_easy_id;
55
struct curltime last_cleanup;
56
struct Curl_easy *idata; /* internal handle for maintenance */
57
struct Curl_share *share; /* != NULL if pool belongs to share */
58
BIT(locked);
59
BIT(initialised);
60
};
61
62
/* Init the pool, pass multi only if pool is owned by it.
63
* Cannot fail.
64
*/
65
void Curl_cpool_init(struct cpool *cpool,
66
struct Curl_easy *idata,
67
struct Curl_share *share,
68
size_t size);
69
70
/* Destroy all connections and free all members */
71
void Curl_cpool_destroy(struct cpool *connc);
72
73
/* Init the transfer to be used within its connection pool.
74
* Assigns `data->id`. */
75
void Curl_cpool_xfer_init(struct Curl_easy *data);
76
77
/* Get the connection with the given id from `data`'s conn pool. */
78
struct connectdata *Curl_cpool_get_conn(struct Curl_easy *data,
79
curl_off_t conn_id);
80
81
/* Add the connection to the pool. */
82
CURLcode Curl_cpool_add(struct Curl_easy *data,
83
struct connectdata *conn) WARN_UNUSED_RESULT;
84
85
/**
86
* Return if the pool has reached its configured limits for adding
87
* the given connection. Will try to discard the oldest, idle
88
* connections to make space.
89
*/
90
#define CPOOL_LIMIT_OK 0
91
#define CPOOL_LIMIT_DEST 1
92
#define CPOOL_LIMIT_TOTAL 2
93
int Curl_cpool_check_limits(struct Curl_easy *data,
94
struct connectdata *conn);
95
96
/* Return of conn is suitable. If so, stops iteration. */
97
typedef bool Curl_cpool_conn_match_cb(struct connectdata *conn,
98
void *userdata);
99
100
/* Act on the result of the find, may override it. */
101
typedef bool Curl_cpool_done_match_cb(bool result, void *userdata);
102
103
/**
104
* Find a connection in the pool matching `destination`.
105
* All callbacks are invoked while the pool's lock is held.
106
* @param data current transfer
107
* @param destination match against `conn->destination` in pool
108
* @param conn_cb must be present, called for each connection in the
109
* bundle until it returns TRUE
110
* @return combined result of last conn_db and result_cb or FALSE if no
111
connections were present.
112
*/
113
bool Curl_cpool_find(struct Curl_easy *data,
114
const char *destination,
115
Curl_cpool_conn_match_cb *conn_cb,
116
Curl_cpool_done_match_cb *done_cb,
117
void *userdata);
118
119
/*
120
* A connection (already in the pool) is now idle. Do any
121
* cleanups in regard to the pool's limits.
122
*
123
* Return TRUE if idle connection kept in pool, FALSE if closed.
124
*/
125
bool Curl_cpool_conn_now_idle(struct Curl_easy *data,
126
struct connectdata *conn);
127
128
/**
129
* This function scans the data's connection pool for half-open/dead
130
* connections, closes and removes them.
131
* The cleanup is done at most once per second.
132
*
133
* When called, this transfer has no connection attached.
134
*/
135
void Curl_cpool_prune_dead(struct Curl_easy *data);
136
137
/**
138
* Perform upkeep actions on connections in the transfer's pool.
139
*/
140
CURLcode Curl_cpool_upkeep(struct Curl_easy *data);
141
142
typedef void Curl_cpool_conn_do_cb(struct connectdata *conn,
143
struct Curl_easy *data,
144
void *cbdata);
145
146
/**
147
* Invoke the callback on the pool's connection with the
148
* given connection id (if it exists).
149
*/
150
void Curl_cpool_do_by_id(struct Curl_easy *data,
151
curl_off_t conn_id,
152
Curl_cpool_conn_do_cb *cb, void *cbdata);
153
154
/**
155
* Invoked the callback for the given data + connection under the
156
* connection pool's lock.
157
* The callback is always invoked, even if the transfer has no connection
158
* pool associated.
159
*/
160
void Curl_cpool_do_locked(struct Curl_easy *data,
161
struct connectdata *conn,
162
Curl_cpool_conn_do_cb *cb, void *cbdata);
163
164
/* Close all unused connections, prevent reuse of existing ones. */
165
void Curl_cpool_nw_changed(struct Curl_easy *data);
166
167
#endif /* HEADER_CURL_CONNCACHE_H */
168
169