26#ifndef MYSQLROUTER_CONNECTION_POOL_INCLUDED
27#define MYSQLROUTER_CONNECTION_POOL_INCLUDED
62 void remover(std::function<
void()> remover) { remover_ = std::move(remover); }
103 (void)
conn_.cancel();
119 tmr.async_wait([
this](std::error_code ec) {
125 (void)
conn_.cancel();
142 conn_.async_recv([
this](std::error_code ec,
size_t ) {
156 conn_.channel().recv_buffer().clear();
194 : conn_(
std::move(conn)) {}
198 void async_send_quit();
200 void await_quit_response(std::error_code ec,
size_t transferred);
206 before_close_ = std::move(cb);
217 std::chrono::milliseconds idle_timeout)
218 : max_pooled_connections_(max_pooled_connections),
219 idle_timeout_(idle_timeout) {}
241 std::optional<ServerSideConnection> add_if_not_full(
249 std::optional<ServerSideConnection>
pop_if(
250 const std::string &ep,
251 std::predicate<const ServerSideConnection &>
auto pred) {
253 [
this, ep, pred](
auto &pool) -> std::optional<ServerSideConnection> {
259 [pred](
const auto &kv) { return pred(kv.second.connection()); });
260 if (kv_it ==
key_range.second)
return std::nullopt;
264 auto pooled_conn = std::move(kv_it->second);
272 return std::move(pooled_conn.connection());
281 [[nodiscard]] uint32_t current_pooled_connections()
const;
284 return max_pooled_connections_;
288 return idle_timeout_;
299 void stash(ServerSideConnection conn, ConnectionIdentifier from,
300 std::chrono::milliseconds delay);
303 void discard_all_stashed(ConnectionIdentifier from);
311 std::chrono::steady_clock::time_point tp)
312 : pooled_conn(
std::move(pc)), conn_id(ci), after(tp) {}
316 std::chrono::steady_clock::time_point
after;
319 std::optional<ServerSideConnection> unstash_if(
320 const std::string &ep,
322 bool ignore_sharing_delay =
false);
324 std::optional<ServerSideConnection> unstash_mine(
330 [[nodiscard]]
size_t current_stashed_connections()
const;
339 std::unordered_multimap<std::string,
341 using stash_type = std::unordered_multimap<std::string, Stashed>;
343 void erase(pool_type::iterator it);
357 void erase_from_stash(stash_type::iterator it);
Definition: connection_pool.h:191
ConnectionPool::ServerSideConnection & connection()
Definition: connection_pool.h:202
ConnectionPool::ServerSideConnection conn_
Definition: connection_pool.h:210
std::function< void(const ConnectionPool::ServerSideConnection &)> before_close_
Definition: connection_pool.h:213
ConnectionCloser(ConnectionPool::ServerSideConnection conn)
Definition: connection_pool.h:193
void before_close(std::function< void(const ConnectionPool::ServerSideConnection &)> cb)
Definition: connection_pool.h:204
connection pool of mysql connections.
Definition: connection_pool.h:182
ConnectionPool & operator=(const ConnectionPool &)=delete
~ConnectionPool()=default
uint32_t max_pooled_connections() const
Definition: connection_pool.h:283
std::chrono::milliseconds idle_timeout() const
Definition: connection_pool.h:287
const uint32_t max_pooled_connections_
Definition: connection_pool.h:345
void * ConnectionIdentifier
Definition: connection_pool.h:189
std::unordered_multimap< std::string, PooledConnection< ServerSideConnection > > pool_type
Definition: connection_pool.h:340
ConnectionPool(uint32_t max_pooled_connections, std::chrono::milliseconds idle_timeout)
Definition: connection_pool.h:216
uint64_t reused_connections() const
total number of reused connections.
Definition: connection_pool.h:335
const std::chrono::milliseconds idle_timeout_
Definition: connection_pool.h:346
std::optional< ServerSideConnection > pop_if(const std::string &ep, std::predicate< const ServerSideConnection & > auto pred)
get a connection from the pool that matches a predicate.
Definition: connection_pool.h:249
ConnectionPool(ConnectionPool &&)=delete
ConnectionPool & operator=(ConnectionPool &&)=delete
std::unordered_multimap< std::string, Stashed > stash_type
Definition: connection_pool.h:341
ConnectionPool(const ConnectionPool &)=delete
Monitor pattern.
Definition: monitor.h:39
pooled connection.
Definition: connection_pool.h:54
void remover(std::function< void()> remover)
set a remove-callback.
Definition: connection_pool.h:62
void remove_me()
calls remove-callback.
Definition: connection_pool.cc:115
std::function< void()> remover_
Definition: connection_pool.h:72
void reset()
Definition: connection_pool.cc:120
pooled connection.
Definition: connection_pool.h:79
connection_type & connection()
access to conn_.
Definition: connection_pool.h:92
T connection_type
Definition: connection_pool.h:82
net::steady_timer idle_timer_
Definition: connection_pool.h:165
PooledConnection(connection_type conn)
Definition: connection_pool.h:84
void async_recv_message()
wait for server message and shutdown.
Definition: connection_pool.h:136
mysql_harness::Ssl Ssl
Definition: connection_pool.h:81
void reset()
prepares for reusing the connection.
Definition: connection_pool.h:99
void async_idle(std::chrono::milliseconds idle_timeout)
wait for idle timeout.
Definition: connection_pool.h:112
const connection_type & connection() const
Definition: connection_pool.h:94
connection_type conn_
Definition: connection_pool.h:163
size_t cancel()
Definition: timer.h:101
size_t expires_after(const duration &d)
Definition: timer.h:114
static constexpr uint32_t kDefaultMaxIdleServerConnectionsBootstrap
Definition: connection_pool.h:49
#define CONNECTION_POOL_EXPORT
Definition: connection_pool_export.h:15
if(!(yy_init))
Definition: lexyy.cc:1144
std::string HARNESS_EXPORT reset()
get 'reset attributes' ESC sequence.
Definition: vt100.cc:37
Container::const_iterator find_if(const Container &c, Find_if &&find_if)
Definition: generic.h:54
std::unique_ptr< SSL, mysql_harness::impl::Deleter_SSL > Ssl
Definition: tls_types.h:48
std::error_condition make_error_condition(net::stream_errc e) noexcept
Definition: buffer.h:107
static mysql_service_status_t add(reference_caching_channel channel, const char *implementation_name) noexcept
Definition: component.cc:127
Definition: gcs_xcom_synode.h:64
connection on the stash.
Definition: connection_pool.h:308
ConnectionIdentifier conn_id
opaque connection identifier
Definition: connection_pool.h:315
Stashed(PooledConnection< ServerSideConnection > pc, ConnectionIdentifier ci, std::chrono::steady_clock::time_point tp)
Definition: connection_pool.h:310
std::chrono::steady_clock::time_point after
stealable after ...
Definition: connection_pool.h:316
PooledConnection< ServerSideConnection > pooled_conn
pooled connection.
Definition: connection_pool.h:314
Definition: my_base.h:1125