Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-10 08:33:53

0001 //
0002 // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 #ifndef BOOST_MYSQL_IMPL_CONNECTION_POOL_IPP
0009 #define BOOST_MYSQL_IMPL_CONNECTION_POOL_IPP
0010 
0011 #pragma once
0012 
0013 #include <boost/mysql/connection_pool.hpp>
0014 
0015 #include <boost/mysql/detail/connection_pool_fwd.hpp>
0016 
0017 #include <boost/mysql/impl/internal/connection_pool/connection_pool_impl.hpp>
0018 
0019 #include <memory>
0020 
0021 void boost::mysql::detail::return_connection(
0022     std::shared_ptr<pool_impl> pool,
0023     connection_node& node,
0024     bool should_reset
0025 ) noexcept
0026 {
0027     // This is safe to be called from any thread, and is noexcept
0028     node.mark_as_collectable(should_reset);
0029 
0030     // If, for any reason, this notification fails, the connection will
0031     // be collected when the next ping is due.
0032     try
0033     {
0034         // A handler to be passed to dispatch. Binds the executor
0035         // and keeps the pool alive
0036         struct dispatch_handler
0037         {
0038             std::shared_ptr<pool_impl> pool_ptr;
0039             connection_node* node_ptr;
0040 
0041             using executor_type = asio::any_io_executor;
0042             executor_type get_executor() const noexcept { return pool_ptr->get_executor(); }
0043 
0044             void operator()() const { node_ptr->notify_collectable(); }
0045         };
0046 
0047         asio::dispatch(dispatch_handler{std::move(pool), &node});
0048     }
0049     catch (...)
0050     {
0051     }
0052 }
0053 
0054 boost::mysql::any_connection& boost::mysql::detail::get_connection(boost::mysql::detail::connection_node& node
0055 ) noexcept
0056 {
0057     return node.connection();
0058 }
0059 
0060 boost::mysql::connection_pool::connection_pool(pool_executor_params&& ex_params, pool_params&& params, int)
0061     : impl_(std::make_shared<detail::pool_impl>(std::move(ex_params), std::move(params)))
0062 {
0063 }
0064 
0065 boost::mysql::connection_pool::executor_type boost::mysql::connection_pool::get_executor() noexcept
0066 {
0067     return impl_->get_executor();
0068 }
0069 
0070 void boost::mysql::connection_pool::async_run_erased(
0071     std::shared_ptr<detail::pool_impl> pool,
0072     asio::any_completion_handler<void(error_code)> handler
0073 )
0074 {
0075     pool->async_run(std::move(handler));
0076 }
0077 
0078 void boost::mysql::connection_pool::async_get_connection_erased(
0079     std::shared_ptr<detail::pool_impl> pool,
0080     std::chrono::steady_clock::duration timeout,
0081     diagnostics* diag,
0082     asio::any_completion_handler<void(error_code, pooled_connection)> handler
0083 )
0084 {
0085     pool->async_get_connection(timeout, diag, std::move(handler));
0086 }
0087 
0088 void boost::mysql::connection_pool::cancel()
0089 {
0090     BOOST_ASSERT(valid());
0091 
0092     // A handler to be passed to dispatch. Binds the executor
0093     // and keeps the pool alive
0094     struct dispatch_handler
0095     {
0096         std::shared_ptr<detail::pool_impl> pool_ptr;
0097 
0098         using executor_type = asio::any_io_executor;
0099         executor_type get_executor() const noexcept { return pool_ptr->get_executor(); }
0100 
0101         void operator()() const { pool_ptr->cancel_unsafe(); }
0102     };
0103 
0104     asio::dispatch(dispatch_handler{impl_});
0105 }
0106 
0107 #endif