MySQL 8.0.39
Source Code Documentation
connection_pool.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQLROUTER_CONNECTION_POOL_INCLUDED
27#define MYSQLROUTER_CONNECTION_POOL_INCLUDED
28
30
31#include <chrono>
32#include <cstdint> // uint32_t
33
34#include <algorithm> // find_if
35#include <list>
36#include <optional>
37
41#include "mysql/harness/tls_types.h" // Ssl
45
46#include "../../routing/src/ssl_mode.h" // TODO(jkneschk)
47
48/**
49 * pooled connection.
50 */
52 public:
54
55 PooledConnection(std::unique_ptr<ConnectionBase> conn)
56 : conn_{std::move(conn)},
57 ssl_{},
58 is_authenticated_{false},
59 endpoint_{conn_->endpoint()} {}
60
61 PooledConnection(std::unique_ptr<ConnectionBase> conn, Ssl ssl)
62 : conn_{std::move(conn)},
63 ssl_{std::move(ssl)},
64 is_authenticated_{true},
65 endpoint_{conn_->endpoint()} {}
66
67 /**
68 * access to conn_.
69 *
70 * allows others to move the connection structs out.
71 */
72 std::unique_ptr<ConnectionBase> &connection() { return conn_; }
73
74 const std::unique_ptr<ConnectionBase> &connection() const { return conn_; }
75
76 /**
77 * access to ssl_.
78 *
79 * allows others to move the Ssl structs out.
80 */
81 Ssl &ssl() { return ssl_; }
82
83 const Ssl &ssl() const { return ssl_; }
84
85 /**
86 * set a remove-callback.
87 *
88 * used when the pooled connection wants to remove itself from the
89 * connection-pool.
90 */
91 void remover(std::function<void()> remover) { remover_ = std::move(remover); }
92
93 /**
94 * prepares for reusing the connection.
95 */
96 void reset();
97
98 friend class ConnectionPool;
99
100 /**
101 * a pooled connection may be authenticated or not.
102 *
103 * not authenticated:
104 *
105 * - connection expects a fresh handshake
106 *
107 * authenticated:
108 *
109 * - connection expects a change-user
110 *
111 */
112 bool is_authenticated() const { return is_authenticated_; }
113
114 std::string endpoint() const { return endpoint_; }
115
116 private:
117 /**
118 * wait for idle timeout.
119 */
120 void async_idle(std::chrono::milliseconds idle_timeout);
121
122 /**
123 * wait for server message and shutdown.
124 */
125 void async_recv_message();
126
127 /**
128 * calls remove-callback.
129 */
130 void remove_me();
131
132 std::unique_ptr<ConnectionBase> conn_;
133 std::function<void()> remover_;
134
136
138 recv_buf_; // recv-buf for async_recv_message()
139 net::steady_timer idle_timer_{conn_->io_ctx()}; // timer for async_idle()
140
141 bool is_authenticated_{false};
142
143 std::string endpoint_;
144};
145
147 public:
149
150 PooledClassicConnection(std::unique_ptr<ConnectionBase> conn)
151 : PooledConnection{std::move(conn)} {}
152
154 std::unique_ptr<ConnectionBase> conn, Ssl ssl, caps_type server_caps,
155 caps_type client_caps,
156 std::optional<classic_protocol::message::server::Greeting>
157 server_greeting,
158 SslMode ssl_mode, std::string username, std::string schema,
159 std::string attributes)
160 : PooledConnection{std::move(conn), std::move(ssl)},
161 server_caps_{server_caps},
162 client_caps_{client_caps},
163 server_greeting_{std::move(server_greeting)},
164 ssl_mode_{ssl_mode},
165 username_{std::move(username)},
166 schema_{std::move(schema)},
167 attributes_{std::move(attributes)} {}
168
169 [[nodiscard]] caps_type client_capabilities() const { return client_caps_; }
170
171 [[nodiscard]] caps_type server_capabilities() const { return server_caps_; }
172
173 [[nodiscard]] caps_type shared_capabilities() const {
174 return server_caps_ & client_caps_;
175 }
176
177 std::optional<classic_protocol::message::server::Greeting> server_greeting()
178 const {
179 return server_greeting_;
180 }
181
182 SslMode ssl_mode() const { return ssl_mode_; }
183
184 std::string username() const { return username_; }
185 std::string schema() const { return schema_; }
186 std::string attributes() const { return attributes_; }
187
188 private:
189 caps_type server_caps_{};
190 caps_type client_caps_{};
191
192 std::optional<classic_protocol::message::server::Greeting> server_greeting_;
193
195
196 std::string username_;
197 std::string schema_;
198 std::string attributes_;
199};
200
201/**
202 * connection pool of mysql connections.
203 *
204 * pool can contain connections:
205 *
206 * - of any protocol.
207 * - to any tcp endpoint.
208 *
209 */
211 public:
213
214 ConnectionPool(uint32_t max_pooled_connections,
215 std::chrono::milliseconds idle_timeout)
216 : max_pooled_connections_(max_pooled_connections),
217 idle_timeout_(idle_timeout) {}
218
219 // disable copy
222
223 // disable move
226
227 ~ConnectionPool() = default;
228
229 void add(connection_type conn);
230
231 /**
232 * add connection to the pool if the poll isn't full.
233 */
234 std::optional<connection_type> add_if_not_full(connection_type conn);
235
236 /**
237 * get a connection from the pool that matches a predicate.
238 *
239 * @returns a connection if one exists.
240 */
241 template <class UnaryPredicate>
242 std::optional<connection_type> pop_if(UnaryPredicate pred) {
243 return pool_(
244 [this,
245 &pred](auto &pool) -> std::optional<ConnectionPool::connection_type> {
246 auto it = std::find_if(pool.begin(), pool.end(), pred);
247
248 if (it == pool.end()) return {};
249
250 auto pooled_conn = std::move(*it);
251
252 pool.erase(it);
253
254 pooled_conn.reset();
255
256 ++reused_;
257
258 return pooled_conn;
259 });
260 }
261
262 /**
263 * number of currently pooled connections.
264 */
265 [[nodiscard]] uint32_t current_pooled_connections() const;
266
267 [[nodiscard]] uint32_t max_pooled_connections() const {
268 return max_pooled_connections_;
269 }
270
271 [[nodiscard]] std::chrono::milliseconds idle_timeout() const {
272 return idle_timeout_;
273 }
274
275 /**
276 * total number of reused connections.
277 */
278 [[nodiscard]] uint64_t reused_connections() const { return reused_; }
279
280 protected:
281 using container_type = std::list<connection_type>;
282
283 void erase(container_type::iterator it);
284
286 const std::chrono::milliseconds idle_timeout_;
287
289
290 uint64_t reused_{};
291};
292
293#endif
std::vector< uint8_t, default_init_allocator< uint8_t > > recv_buffer_type
Definition: connection_base.h:47
connection pool of mysql connections.
Definition: connection_pool.h:210
ConnectionPool & operator=(const ConnectionPool &)=delete
~ConnectionPool()=default
uint32_t max_pooled_connections() const
Definition: connection_pool.h:267
std::chrono::milliseconds idle_timeout() const
Definition: connection_pool.h:271
const uint32_t max_pooled_connections_
Definition: connection_pool.h:285
std::list< connection_type > container_type
Definition: connection_pool.h:281
ConnectionPool(uint32_t max_pooled_connections, std::chrono::milliseconds idle_timeout)
Definition: connection_pool.h:214
uint64_t reused_connections() const
total number of reused connections.
Definition: connection_pool.h:278
const std::chrono::milliseconds idle_timeout_
Definition: connection_pool.h:286
ConnectionPool(ConnectionPool &&)=delete
ConnectionPool & operator=(ConnectionPool &&)=delete
std::optional< connection_type > pop_if(UnaryPredicate pred)
get a connection from the pool that matches a predicate.
Definition: connection_pool.h:242
ConnectionPool(const ConnectionPool &)=delete
Monitor pattern.
Definition: monitor.h:39
Definition: connection_pool.h:146
std::string attributes_
Definition: connection_pool.h:198
std::string schema_
Definition: connection_pool.h:197
PooledClassicConnection(std::unique_ptr< ConnectionBase > conn, Ssl ssl, caps_type server_caps, caps_type client_caps, std::optional< classic_protocol::message::server::Greeting > server_greeting, SslMode ssl_mode, std::string username, std::string schema, std::string attributes)
Definition: connection_pool.h:153
SslMode ssl_mode() const
Definition: connection_pool.h:182
std::string username_
Definition: connection_pool.h:196
PooledClassicConnection(std::unique_ptr< ConnectionBase > conn)
Definition: connection_pool.h:150
caps_type client_capabilities() const
Definition: connection_pool.h:169
std::optional< classic_protocol::message::server::Greeting > server_greeting() const
Definition: connection_pool.h:177
SslMode ssl_mode_
Definition: connection_pool.h:194
caps_type shared_capabilities() const
Definition: connection_pool.h:173
std::string schema() const
Definition: connection_pool.h:185
caps_type server_capabilities() const
Definition: connection_pool.h:171
classic_protocol::capabilities::value_type caps_type
Definition: connection_pool.h:148
std::optional< classic_protocol::message::server::Greeting > server_greeting_
Definition: connection_pool.h:192
std::string username() const
Definition: connection_pool.h:184
std::string attributes() const
Definition: connection_pool.h:186
pooled connection.
Definition: connection_pool.h:51
std::unique_ptr< ConnectionBase > & connection()
access to conn_.
Definition: connection_pool.h:72
Ssl ssl_
Definition: connection_pool.h:135
const std::unique_ptr< ConnectionBase > & connection() const
Definition: connection_pool.h:74
ConnectionBase::recv_buffer_type recv_buf_
Definition: connection_pool.h:138
PooledConnection(std::unique_ptr< ConnectionBase > conn)
Definition: connection_pool.h:55
PooledConnection(std::unique_ptr< ConnectionBase > conn, Ssl ssl)
Definition: connection_pool.h:61
Ssl & ssl()
access to ssl_.
Definition: connection_pool.h:81
mysql_harness::Ssl Ssl
Definition: connection_pool.h:53
bool is_authenticated() const
a pooled connection may be authenticated or not.
Definition: connection_pool.h:112
const Ssl & ssl() const
Definition: connection_pool.h:83
std::string endpoint_
Definition: connection_pool.h:143
std::function< void()> remover_
Definition: connection_pool.h:133
void remover(std::function< void()> remover)
set a remove-callback.
Definition: connection_pool.h:91
std::unique_ptr< ConnectionBase > conn_
Definition: connection_pool.h:132
std::string endpoint() const
Definition: connection_pool.h:114
Definition: timer.h:57
#define CONNECTION_POOL_EXPORT
Definition: connection_pool_export.h:15
std::string HARNESS_EXPORT reset()
get 'reset attributes' ESC sequence.
Definition: vt100.cc:37
constexpr value_type ssl
Definition: classic_protocol_constants.h:49
std::bitset< 32 > value_type
Definition: classic_protocol_constants.h:73
std::unique_ptr< SSL, mysql_harness::impl::Deleter_SSL > Ssl
Definition: tls_types.h:48
static mysql_service_status_t add(reference_caching_channel channel, const char *implementation_name) noexcept
Definition: component.cc:135
Definition: gcs_xcom_synode.h:64
SslMode
Definition: ssl_mode.h:29