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) {
141 conn_.async_recv([
this](std::error_code ec,
size_t ) {
155 conn_.channel().recv_buffer().clear();
191 std::chrono::milliseconds idle_timeout)
192 : max_pooled_connections_(max_pooled_connections),
193 idle_timeout_(idle_timeout) {}
215 std::optional<ServerSideConnection> add_if_not_full(
223 std::optional<ServerSideConnection>
pop_if(
224 const std::string &ep,
225 std::predicate<const ServerSideConnection &>
auto pred) {
227 [
this, ep, pred](
auto &pool) -> std::optional<ServerSideConnection> {
233 [pred](
const auto &kv) { return pred(kv.second.connection()); });
234 if (kv_it ==
key_range.second)
return std::nullopt;
238 auto pooled_conn = std::move(kv_it->second);
246 return std::move(pooled_conn.connection());
253 [[nodiscard]] uint32_t current_pooled_connections()
const;
256 return max_pooled_connections_;
260 return idle_timeout_;
271 void stash(ServerSideConnection conn, ConnectionIdentifier from,
272 std::chrono::milliseconds delay);
275 void discard_all_stashed(ConnectionIdentifier from);
283 std::chrono::steady_clock::time_point tp)
284 : pooled_conn(
std::move(pc)), conn_id(ci), after(tp) {}
288 std::chrono::steady_clock::time_point
after;
291 std::optional<ServerSideConnection> unstash_if(
292 const std::string &ep,
294 bool ignore_sharing_delay =
false);
296 std::optional<ServerSideConnection> unstash_mine(
302 [[nodiscard]]
size_t current_stashed_connections()
const;
311 std::unordered_multimap<std::string,
313 using stash_type = std::unordered_multimap<std::string, Stashed>;
315 void erase(pool_type::iterator it);
327 void erase_from_stash(stash_type::iterator it);
connection pool of mysql connections.
Definition: connection_pool.h:181
ConnectionPool & operator=(const ConnectionPool &)=delete
~ConnectionPool()=default
uint32_t max_pooled_connections() const
Definition: connection_pool.h:255
std::chrono::milliseconds idle_timeout() const
Definition: connection_pool.h:259
const uint32_t max_pooled_connections_
Definition: connection_pool.h:317
void * ConnectionIdentifier
Definition: connection_pool.h:188
std::unordered_multimap< std::string, PooledConnection< ServerSideConnection > > pool_type
Definition: connection_pool.h:312
ConnectionPool(uint32_t max_pooled_connections, std::chrono::milliseconds idle_timeout)
Definition: connection_pool.h:190
uint64_t reused_connections() const
total number of reused connections.
Definition: connection_pool.h:307
const std::chrono::milliseconds idle_timeout_
Definition: connection_pool.h:318
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:223
ConnectionPool(ConnectionPool &&)=delete
ConnectionPool & operator=(ConnectionPool &&)=delete
std::unordered_multimap< std::string, Stashed > stash_type
Definition: connection_pool.h:313
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:98
std::function< void()> remover_
Definition: connection_pool.h:72
void reset()
Definition: connection_pool.cc:103
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:164
PooledConnection(connection_type conn)
Definition: connection_pool.h:84
void async_recv_message()
wait for server message and shutdown.
Definition: connection_pool.h:135
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:162
size_t cancel()
Definition: timer.h:101
size_t expires_after(const duration &d)
Definition: timer.h:114
static void connection_quit(ConnectionBase *conn, SSL *ssl)
Definition: connection_pool.cc:43
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:280
ConnectionIdentifier conn_id
opaque connection identifier
Definition: connection_pool.h:287
Stashed(PooledConnection< ServerSideConnection > pc, ConnectionIdentifier ci, std::chrono::steady_clock::time_point tp)
Definition: connection_pool.h:282
std::chrono::steady_clock::time_point after
stealable after ...
Definition: connection_pool.h:288
PooledConnection< ServerSideConnection > pooled_conn
pooled connection.
Definition: connection_pool.h:286
Definition: my_base.h:1125