From d73fc6516efcdf4218beae0f4dbfe488e010e7ad Mon Sep 17 00:00:00 2001 From: Ruben Perez Date: Thu, 7 Dec 2023 21:50:29 +0100 Subject: [PATCH] Simplified run_with_timeout --- .../connection_pool/run_with_timeout.hpp | 42 ++++--------------- 1 file changed, 7 insertions(+), 35 deletions(-) diff --git a/include/boost/mysql/detail/connection_pool/run_with_timeout.hpp b/include/boost/mysql/detail/connection_pool/run_with_timeout.hpp index 0985c4dbd..5b5651c4e 100644 --- a/include/boost/mysql/detail/connection_pool/run_with_timeout.hpp +++ b/include/boost/mysql/detail/connection_pool/run_with_timeout.hpp @@ -12,11 +12,9 @@ #include #include -#include #include #include #include -#include #include #include @@ -41,45 +39,19 @@ inline error_code to_error_code( return client_errc::cancelled; } -// Timer's expiry should be set by the caller -template -void run_with_timeout_impl(TimerType& timer, Op&& op, Handler&& handler) -{ - auto adapter = [](std::array completion_order, error_code io_ec, error_code timer_ec) { - return asio::deferred.values(to_error_code(completion_order, io_ec, timer_ec)); - }; - asio::experimental::make_parallel_group(std::forward(op), timer.async_wait(asio::deferred)) - .async_wait(asio::experimental::wait_for_one(), asio::deferred(adapter))(std::forward(handler - )); -} - -template -void run_with_timeout( - TimerType* timer, - boost::optional tp, - Op&& op, - Handler&& handler -) -{ - if (tp) - { - BOOST_ASSERT(timer != nullptr); - timer->expires_at(*tp); - run_with_timeout_impl(*timer, std::forward(op), std::forward(handler)); - } - else - { - std::forward(op)(std::forward(handler)); - } -} - template void run_with_timeout(TimerType& timer, std::chrono::steady_clock::duration dur, Op&& op, Handler&& handler) { if (dur.count() > 0) { timer.expires_after(dur); - run_with_timeout_impl(timer, std::forward(op), std::forward(handler)); + auto adapter = [](std::array completion_order, error_code io_ec, error_code timer_ec + ) { return asio::deferred.values(to_error_code(completion_order, io_ec, timer_ec)); }; + + asio::experimental::make_parallel_group(std::forward(op), timer.async_wait(asio::deferred)) + .async_wait(asio::experimental::wait_for_one(), asio::deferred(adapter))( + std::forward(handler) + ); } else {