pool_params.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // Copyright (c) 2019-2024 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/mysql/detail/access.hpp>
  13. #include <boost/asio/any_io_executor.hpp>
  14. #include <boost/asio/ssl/context.hpp>
  15. #include <boost/asio/strand.hpp>
  16. #include <boost/optional/optional.hpp>
  17. #include <chrono>
  18. #include <cstddef>
  19. #include <string>
  20. namespace boost {
  21. namespace mysql {
  22. /**
  23. * \brief (EXPERIMENTAL) Executor configuration for connection pools.
  24. * \details
  25. * Contains two executors: one for the pool's internal objects, and another for the connections
  26. * created by the pool.
  27. * \n
  28. * You may use \ref thread_safe to create an instance of this class
  29. * that makes pools thread-safe.
  30. *
  31. * \par Experimental
  32. * This part of the API is experimental, and may change in successive
  33. * releases without previous notice.
  34. */
  35. struct pool_executor_params
  36. {
  37. /// The executor to be used by the pool's internal objects.
  38. asio::any_io_executor pool_executor;
  39. /// The executor to be used by connections created by the pool.
  40. asio::any_io_executor connection_executor;
  41. /**
  42. * \brief Creates a pool_executor_params object that makes pools thread-safe.
  43. * \details
  44. * Creates an `asio::strand` object wrapping `ex` and uses it as the pool
  45. * executor. Uses `ex` directly for the connections. The resulting configuration
  46. * makes safe to call \ref connection_pool::async_get_connection,
  47. * \ref connection_pool::async_run, \ref connection_pool::cancel,
  48. * `~pooled_connection` and \ref pooled_connection::return_without_reset
  49. * concurrently from different threads.
  50. *
  51. * \par Exception safety
  52. * Strong guarantee. Creating the strand may throw.
  53. */
  54. static pool_executor_params thread_safe(asio::any_io_executor ex)
  55. {
  56. return pool_executor_params{asio::make_strand(ex), ex};
  57. }
  58. };
  59. /**
  60. * \brief (EXPERIMENTAL) Configuration parameters for \ref connection_pool.
  61. * \details
  62. * This is an owning type.
  63. *
  64. * \par Experimental
  65. * This part of the API is experimental, and may change in successive
  66. * releases without previous notice.
  67. */
  68. struct pool_params
  69. {
  70. /**
  71. * \brief Determines how to establish a physical connection to the MySQL server.
  72. * \details
  73. * Connections created by the pool will use this address to connect to the
  74. * server. This can be either a host and port or a UNIX socket path.
  75. * Defaults to (localhost, 3306).
  76. */
  77. any_address server_address;
  78. /// User name that connections created by the pool should use to authenticate as.
  79. std::string username;
  80. /// Password that connections created by the pool should use.
  81. std::string password;
  82. /**
  83. * \brief Database name that connections created by the pool will use when connecting.
  84. * \details Leave it empty to select no database (this is the default).
  85. */
  86. std::string database;
  87. /**
  88. * \brief Controls whether connections created by the pool will use TLS or not.
  89. * \details
  90. * See \ref ssl_mode for more information about the possible modes.
  91. * This option is only relevant when `server_address.type() == address_type::host_and_port`.
  92. * UNIX socket connections will never use TLS, regardless of this value.
  93. */
  94. ssl_mode ssl{ssl_mode::enable};
  95. /**
  96. * \brief Whether to enable support for semicolon-separated text queries for connections created by the
  97. * pool. \details Disabled by default.
  98. */
  99. bool multi_queries{false};
  100. /// Initial size (in bytes) of the internal buffer for the connections created by the pool.
  101. std::size_t initial_buffer_size{default_initial_read_buffer_size};
  102. /**
  103. * \brief Initial number of connections to create.
  104. * \details
  105. * When \ref connection_pool::async_run starts running, this number of connections
  106. * will be created and connected.
  107. */
  108. std::size_t initial_size{1};
  109. /**
  110. * \brief Max number of connections to create.
  111. * \details
  112. * When a connection is requested, but all connections are in use, new connections
  113. * will be created and connected up to this size.
  114. * \n
  115. * Defaults to the maximum number of concurrent connections that MySQL
  116. * servers allow by default. If you increase this value, increase the server's
  117. * max number of connections, too (by setting the `max_connections` global variable).
  118. * \n
  119. * This value must be `> 0` and `>= initial_size`.
  120. */
  121. std::size_t max_size{151};
  122. /**
  123. * \brief The SSL context to use for connections using TLS.
  124. * \details
  125. * If a non-empty value is provided, all connections created by the pool
  126. * will use the passed context when using TLS. This allows setting TLS options
  127. * to pool-created connections.
  128. * \n
  129. * If an empty value is passed (the default) and the connections require TLS,
  130. * an internal SSL context with suitable options will be created by the pool.
  131. */
  132. boost::optional<asio::ssl::context> ssl_ctx{};
  133. /**
  134. * \brief The timeout to use when connecting.
  135. * \details
  136. * Connections will be connected by the pool before being handed to the user
  137. * (using \ref any_connection::async_connect).
  138. * If the operation takes longer than this timeout,
  139. * the operation will be interrupted, considered as failed and retried later.
  140. * \n
  141. * Set this timeout to zero to disable it.
  142. * \n
  143. * This value must not be negative.
  144. */
  145. std::chrono::steady_clock::duration connect_timeout{std::chrono::seconds(20)};
  146. /**
  147. * \brief The interval between connect attempts.
  148. * \details
  149. * When session establishment fails, the operation will be retried until
  150. * success. This value determines the interval between consecutive connection
  151. * attempts.
  152. * \n
  153. * This value must be greater than zero.
  154. */
  155. std::chrono::steady_clock::duration retry_interval{std::chrono::seconds(30)};
  156. /**
  157. * \brief The health-check interval.
  158. * \details
  159. * If a connection becomes idle and hasn't been handed to the user for
  160. * `ping_interval`, a health-check will be performed (using \ref any_connection::async_ping).
  161. * Pings will be sent with a periodicity of `ping_interval` until the connection
  162. * is handed to the user, or a ping fails.
  163. * \n
  164. * Set this interval to zero to disable pings.
  165. * \n
  166. * This value must not be negative.
  167. */
  168. std::chrono::steady_clock::duration ping_interval{std::chrono::hours(1)};
  169. /**
  170. * \brief The timeout to use for pings and session resets.
  171. * \details
  172. * If pings (as per \ref any_connection::async_ping) or session resets
  173. * (as per \ref any_connection::async_reset_connection) take longer than this
  174. * timeout, they will be cancelled, and the operation will be considered failed.
  175. * \n
  176. * Set this timeout to zero to disable it.
  177. * \n
  178. * This value must not be negative.
  179. */
  180. std::chrono::steady_clock::duration ping_timeout{std::chrono::seconds(10)};
  181. };
  182. } // namespace mysql
  183. } // namespace boost
  184. #endif