9#include <boost/asio.hpp>
10#if defined(BOOST_ASIO_HAS_CO_AWAIT)
11#include <boost/asio/experimental/awaitable_operators.hpp>
14namespace net = boost::asio;
15using namespace net::experimental::awaitable_operators;
16using resolver = net::use_awaitable_t<>::as_default_on_t<net::ip::tcp::resolver>;
17using timer_type = net::use_awaitable_t<>::as_default_on_t<net::steady_timer>;
24auto redir(boost::system::error_code& ec)
25 {
return net::redirect_error(net::use_awaitable, ec); }
28auto healthy_checker(std::shared_ptr<connection> conn) -> net::awaitable<void>
34 timer_type timer{
co_await net::this_coro::executor};
36 for (boost::system::error_code ec;;) {
37 timer.expires_after(std::chrono::seconds{1});
38 co_await (conn->async_exec(req,
adapt()) || timer.async_wait(redir(ec)));
45 timer.expires_after(std::chrono::seconds{1});
46 co_await timer.async_wait();
54 std::shared_ptr<connection> conn,
55 std::string
const& host,
56 std::string
const& port) -> net::awaitable<void>
58 auto ex =
co_await net::this_coro::executor;
62 boost::system::error_code ec;
63 timer.expires_after(std::chrono::seconds{5});
64 auto const addrs =
co_await (resv.async_resolve(host, port) || timer.async_wait(redir(ec)));
66 throw std::runtime_error(
"Resolve timeout");
68 timer.expires_after(std::chrono::seconds{5});
69 co_await (net::async_connect(conn->next_layer(), std::get<0>(addrs)) || timer.async_wait(redir(ec)));
71 throw std::runtime_error(
"Connect timeout");
74auto run(net::awaitable<void> op) ->
int
78 net::co_spawn(ioc, std::move(op), [](std::exception_ptr p) {
80 std::rethrow_exception(p);
86 }
catch (std::exception
const& e) {
87 std::cerr <<
"Error: " << e.what() << std::endl;
auto adapt(std::size_t max_read_size=(std::numeric_limits< std::size_t >::max)()) noexcept
Creates an adapter that ignores responses.
operation
Connection operations that can be cancelled.
@ run
Refers to connection::async_run operations.