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