pool_params.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // Copyright (c) 2019-2025 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_POOL_PARAMS_HPP
  8. #define BOOST_MYSQL_POOL_PARAMS_HPP
  9. #include <boost/mysql/any_address.hpp>
  10. #include <boost/mysql/defaults.hpp>
  11. #include <boost/mysql/ssl_mode.hpp>
  12. #include <boost/asio/any_io_executor.hpp>
  13. #include <boost/asio/ssl/context.hpp>
  14. #include <boost/optional/optional.hpp>
  15. #include <chrono>
  16. #include <cstddef>
  17. #include <string>
  18. namespace boost {
  19. namespace mysql {
  20. /**
  21. * \brief Configuration parameters for \ref connection_pool.
  22. * \details
  23. * This is an owning type.
  24. */
  25. struct pool_params
  26. {
  27. /**
  28. * \brief Determines how to establish a physical connection to the MySQL server.
  29. * \details
  30. * Connections created by the pool will use this address to connect to the
  31. * server. This can be either a host and port or a UNIX socket path.
  32. * Defaults to (localhost, 3306).
  33. */
  34. any_address server_address;
  35. /// User name that connections created by the pool should use to authenticate as.
  36. std::string username;
  37. /// Password that connections created by the pool should use.
  38. std::string password;
  39. /**
  40. * \brief Database name that connections created by the pool will use when connecting.
  41. * \details Leave it empty to select no database (this is the default).
  42. */
  43. std::string database;
  44. /**
  45. * \brief Controls whether connections created by the pool will use TLS or not.
  46. * \details
  47. * See \ref ssl_mode for more information about the possible modes.
  48. * This option is only relevant when `server_address.type() == address_type::host_and_port`.
  49. * UNIX socket connections will never use TLS, regardless of this value.
  50. */
  51. ssl_mode ssl{ssl_mode::enable};
  52. /**
  53. * \brief Whether to enable support for semicolon-separated text queries for connections created by the
  54. * pool. \details Disabled by default.
  55. */
  56. bool multi_queries{false};
  57. /// Initial size (in bytes) of the internal buffer for the connections created by the pool.
  58. std::size_t initial_buffer_size{default_initial_read_buffer_size};
  59. /**
  60. * \brief Initial number of connections to create.
  61. * \details
  62. * When \ref connection_pool::async_run starts running, this number of connections
  63. * will be created and connected.
  64. */
  65. std::size_t initial_size{1};
  66. /**
  67. * \brief Max number of connections to create.
  68. * \details
  69. * When a connection is requested, but all connections are in use, new connections
  70. * will be created and connected up to this size.
  71. * \n
  72. * Defaults to the maximum number of concurrent connections that MySQL
  73. * servers allow by default. If you increase this value, increase the server's
  74. * max number of connections, too (by setting the `max_connections` global variable).
  75. * \n
  76. * This value must be `> 0` and `>= initial_size`.
  77. */
  78. std::size_t max_size{151};
  79. /**
  80. * \brief The SSL context to use for connections using TLS.
  81. * \details
  82. * If a non-empty value is provided, all connections created by the pool
  83. * will use the passed context when using TLS. This allows setting TLS options
  84. * to pool-created connections.
  85. * \n
  86. * If an empty value is passed (the default) and the connections require TLS,
  87. * an internal SSL context with suitable options will be created by the pool.
  88. */
  89. boost::optional<asio::ssl::context> ssl_ctx{};
  90. /**
  91. * \brief The timeout to use when connecting.
  92. * \details
  93. * Connections will be connected by the pool before being handed to the user
  94. * (using \ref any_connection::async_connect).
  95. * If the operation takes longer than this timeout,
  96. * the operation will be interrupted, considered as failed and retried later.
  97. * \n
  98. * Set this timeout to zero to disable it.
  99. * \n
  100. * This value must not be negative.
  101. */
  102. std::chrono::steady_clock::duration connect_timeout{std::chrono::seconds(20)};
  103. /**
  104. * \brief The interval between connect attempts.
  105. * \details
  106. * When session establishment fails, the operation will be retried until
  107. * success. This value determines the interval between consecutive connection
  108. * attempts.
  109. * \n
  110. * This value must be greater than zero.
  111. */
  112. std::chrono::steady_clock::duration retry_interval{std::chrono::seconds(30)};
  113. /**
  114. * \brief The health-check interval.
  115. * \details
  116. * If a connection becomes idle and hasn't been handed to the user for
  117. * `ping_interval`, a health-check will be performed (using \ref any_connection::async_ping).
  118. * Pings will be sent with a periodicity of `ping_interval` until the connection
  119. * is handed to the user, or a ping fails.
  120. *
  121. * Set this interval to zero to disable pings.
  122. *
  123. * This value must not be negative. It should be smaller than the server's
  124. * idle timeout (as determined by the
  125. * <a
  126. * href="https://dev.mysql.com/doc/refman/8.4/en/server-system-variables.html#sysvar_wait_timeout">wait_timeout</a>
  127. * session variable). Otherwise, the server might close connections
  128. * without the pool detecting it.
  129. */
  130. std::chrono::steady_clock::duration ping_interval{std::chrono::hours(1)};
  131. /**
  132. * \brief The timeout to use for pings and session resets.
  133. * \details
  134. * If pings (as per \ref any_connection::async_ping) or session resets
  135. * (as per \ref any_connection::async_reset_connection) take longer than this
  136. * timeout, they will be cancelled, and the operation will be considered failed.
  137. * \n
  138. * Set this timeout to zero to disable it.
  139. * \n
  140. * This value must not be negative.
  141. */
  142. std::chrono::steady_clock::duration ping_timeout{std::chrono::seconds(10)};
  143. /**
  144. * \brief Enables or disables thread-safety.
  145. * \details
  146. * When set to `true`, the resulting connection pool are able to
  147. * be shared between threads at the cost of some performance.
  148. *
  149. * Enabling thread safety for a pool creates an internal `asio::strand` object
  150. * wrapping the executor passed to the pool's constructor.
  151. * All state-mutating functions (including \ref connection_pool::async_run,
  152. * \ref connection_pool::async_get_connection and returning connections)
  153. * will be run through the created strand.
  154. *
  155. * Thread-safety doesn't extend to individual connections: \ref pooled_connection
  156. * objects can't be shared between threads. Thread-safety does not protect
  157. * objects that don't belong to the pool. For instance, `asio::cancel_after`
  158. * creates a timer that must be protected with a strand.
  159. * Refer to
  160. * <a href="../../connection_pool.html#mysql.connection_pool.thread_safe">this
  161. * page</a> for more info.
  162. */
  163. bool thread_safe{false};
  164. /**
  165. * \brief The executor to be used by individual connections created by the pool.
  166. * \details
  167. * If this member is set to a non-empty value
  168. * (that is, `static_cast<bool>(connection_executor) == true`),
  169. * individual connections will be created using this executor.
  170. * Otherwise, connections will use the pool's executor
  171. * (as per \ref connection_pool::get_executor).
  172. */
  173. asio::any_io_executor connection_executor{};
  174. };
  175. } // namespace mysql
  176. } // namespace boost
  177. #endif