connection_pool.ipp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_IMPL_CONNECTION_POOL_IPP
  8. #define BOOST_MYSQL_IMPL_CONNECTION_POOL_IPP
  9. #pragma once
  10. #include <boost/mysql/connection_pool.hpp>
  11. #include <boost/mysql/detail/connection_pool_fwd.hpp>
  12. #include <boost/mysql/impl/internal/connection_pool/connection_pool_impl.hpp>
  13. #include <memory>
  14. void boost::mysql::detail::return_connection(
  15. std::shared_ptr<pool_impl> pool,
  16. connection_node& node,
  17. bool should_reset
  18. ) noexcept
  19. {
  20. // This is safe to be called from any thread, and is noexcept
  21. node.mark_as_collectable(should_reset);
  22. // If, for any reason, this notification fails, the connection will
  23. // be collected when the next ping is due.
  24. try
  25. {
  26. // A handler to be passed to dispatch. Binds the executor
  27. // and keeps the pool alive
  28. struct dispatch_handler
  29. {
  30. std::shared_ptr<pool_impl> pool_ptr;
  31. connection_node* node_ptr;
  32. using executor_type = asio::any_io_executor;
  33. executor_type get_executor() const noexcept { return pool_ptr->get_executor(); }
  34. void operator()() const { node_ptr->notify_collectable(); }
  35. };
  36. asio::dispatch(dispatch_handler{std::move(pool), &node});
  37. }
  38. catch (...)
  39. {
  40. }
  41. }
  42. boost::mysql::any_connection& boost::mysql::detail::get_connection(boost::mysql::detail::connection_node& node
  43. ) noexcept
  44. {
  45. return node.connection();
  46. }
  47. boost::mysql::connection_pool::connection_pool(pool_executor_params&& ex_params, pool_params&& params, int)
  48. : impl_(std::make_shared<detail::pool_impl>(std::move(ex_params), std::move(params)))
  49. {
  50. }
  51. boost::mysql::connection_pool::executor_type boost::mysql::connection_pool::get_executor() noexcept
  52. {
  53. return impl_->get_executor();
  54. }
  55. void boost::mysql::connection_pool::async_run_erased(
  56. std::shared_ptr<detail::pool_impl> pool,
  57. asio::any_completion_handler<void(error_code)> handler
  58. )
  59. {
  60. pool->async_run(std::move(handler));
  61. }
  62. void boost::mysql::connection_pool::async_get_connection_erased(
  63. std::shared_ptr<detail::pool_impl> pool,
  64. std::chrono::steady_clock::duration timeout,
  65. diagnostics* diag,
  66. asio::any_completion_handler<void(error_code, pooled_connection)> handler
  67. )
  68. {
  69. pool->async_get_connection(timeout, diag, std::move(handler));
  70. }
  71. void boost::mysql::connection_pool::cancel()
  72. {
  73. BOOST_ASSERT(valid());
  74. // A handler to be passed to dispatch. Binds the executor
  75. // and keeps the pool alive
  76. struct dispatch_handler
  77. {
  78. std::shared_ptr<detail::pool_impl> pool_ptr;
  79. using executor_type = asio::any_io_executor;
  80. executor_type get_executor() const noexcept { return pool_ptr->get_executor(); }
  81. void operator()() const { pool_ptr->cancel_unsafe(); }
  82. };
  83. asio::dispatch(dispatch_handler{impl_});
  84. }
  85. #endif