7#ifndef BOOST_REDIS_RUNNER_HPP
8#define BOOST_REDIS_RUNNER_HPP
10#include <boost/redis/detail/health_checker.hpp>
11#include <boost/redis/config.hpp>
12#include <boost/redis/response.hpp>
13#include <boost/redis/detail/helper.hpp>
14#include <boost/redis/error.hpp>
15#include <boost/redis/logger.hpp>
16#include <boost/redis/operation.hpp>
17#include <boost/redis/detail/connector.hpp>
18#include <boost/redis/detail/resolver.hpp>
19#include <boost/redis/detail/handshaker.hpp>
20#include <boost/asio/compose.hpp>
21#include <boost/asio/connect.hpp>
22#include <boost/asio/coroutine.hpp>
23#include <boost/asio/experimental/parallel_group.hpp>
24#include <boost/asio/ip/tcp.hpp>
25#include <boost/asio/steady_timer.hpp>
30namespace boost::redis::detail
33void push_hello(config
const& cfg, request& req);
35template <
class Runner,
class Connection,
class Logger>
37 Runner* runner_ =
nullptr;
38 Connection* conn_ =
nullptr;
40 asio::coroutine coro_{};
43 void operator()(Self& self, system::error_code ec = {}, std::size_t = 0)
45 BOOST_ASIO_CORO_REENTER (coro_)
50 conn_->async_exec(runner_->hello_req_, runner_->hello_resp_, std::move(self));
51 logger_.on_hello(ec, runner_->hello_resp_);
53 if (ec || runner_->has_error_in_response() || is_cancelled(self)) {
54 logger_.trace(
"hello-op: error/canceled. Exiting ...");
56 self.complete(!!ec ? ec : asio::error::operation_aborted);
65template <
class Runner,
class Connection,
class Logger>
68 Runner* runner_ =
nullptr;
69 Connection* conn_ =
nullptr;
71 asio::coroutine coro_{};
74 runner_op(Runner* runner, Connection* conn, Logger l)
81 void operator()( Self& self
82 , std::array<std::size_t, 3> order = {}
83 , system::error_code ec0 = {}
84 , system::error_code ec1 = {}
85 , system::error_code ec2 = {}
88 BOOST_ASIO_CORO_REENTER (coro_)
91 asio::experimental::make_parallel_group(
92 [
this](
auto token) {
return runner_->async_run_all(*conn_, logger_, token); },
93 [
this](
auto token) {
return runner_->health_checker_.async_check_health(*conn_, logger_, token); },
94 [
this](
auto token) {
return runner_->async_hello(*conn_, logger_, token); }
96 asio::experimental::wait_for_all(),
99 logger_.on_runner(ec0, ec1, ec2);
101 if (is_cancelled(self)) {
102 self.complete(asio::error::operation_aborted);
111 if (order[0] == 2 && !!ec2) {
126template <
class Runner,
class Connection,
class Logger>
128 Runner* runner_ =
nullptr;
129 Connection* conn_ =
nullptr;
131 asio::coroutine coro_{};
133 template <
class Self>
134 void operator()(Self& self, system::error_code ec = {}, std::size_t = 0)
136 BOOST_ASIO_CORO_REENTER (coro_)
138 BOOST_ASIO_CORO_YIELD
139 runner_->resv_.async_resolve(std::move(self));
140 logger_.on_resolve(ec, runner_->resv_.results());
143 BOOST_ASIO_CORO_YIELD
144 runner_->ctor_.async_connect(conn_->next_layer().next_layer(), runner_->resv_.results(), std::move(self));
145 logger_.on_connect(ec, runner_->ctor_.endpoint());
148 if (conn_->use_ssl()) {
149 BOOST_ASIO_CORO_YIELD
150 runner_->hsher_.async_handshake(conn_->next_layer(), std::move(self));
151 logger_.on_ssl_handshake(ec);
155 BOOST_ASIO_CORO_YIELD
156 conn_->async_run_lean(runner_->cfg_, logger_, std::move(self));
157 BOOST_REDIS_CHECK_OP0(;)
163template <
class Executor>
166 runner(Executor ex, config cfg)
170 , health_checker_{ex}
179 health_checker_.cancel(op);
183 void set_config(config
const& cfg)
186 resv_.set_config(cfg);
187 ctor_.set_config(cfg);
188 hsher_.set_config(cfg);
189 health_checker_.set_config(cfg);
192 template <
class Connection,
class Logger,
class CompletionToken>
193 auto async_run(Connection& conn, Logger l, CompletionToken token)
195 return asio::async_compose
197 , void(system::error_code)
198 >(runner_op<runner, Connection, Logger>{
this, &conn, l}, token, conn);
201 config
const& get_config() const noexcept {
return cfg_;}
204 using resolver_type = resolver<Executor>;
205 using connector_type = connector<Executor>;
206 using handshaker_type = detail::handshaker<Executor>;
207 using health_checker_type = health_checker<Executor>;
208 using timer_type =
typename connector_type::timer_type;
210 template <
class,
class,
class>
friend struct run_all_op;
211 template <
class,
class,
class>
friend class runner_op;
212 template <
class,
class,
class>
friend struct hello_op;
214 template <
class Connection,
class Logger,
class CompletionToken>
215 auto async_run_all(Connection& conn, Logger l, CompletionToken token)
217 return asio::async_compose
219 , void(system::error_code)
220 >(run_all_op<runner, Connection, Logger>{
this, &conn, l}, token, conn);
223 template <
class Connection,
class Logger,
class CompletionToken>
224 auto async_hello(Connection& conn, Logger l, CompletionToken token)
226 return asio::async_compose
228 , void(system::error_code)
229 >(hello_op<runner, Connection, Logger>{
this, &conn, l}, token, conn);
235 if (hello_resp_.has_value())
236 hello_resp_.value().clear();
237 push_hello(cfg_, hello_req_);
240 bool has_error_in_response() const noexcept
242 if (!hello_resp_.has_value())
245 auto f = [](
auto const& e)
247 switch (e.data_type) {
250 default:
return false;
254 return std::any_of(std::cbegin(hello_resp_.value()), std::cend(hello_resp_.value()), f);
258 connector_type ctor_;
259 handshaker_type hsher_;
260 health_checker_type health_checker_;
adapter::result< std::vector< resp3::node > > generic_response
A generic response to a request.
operation
Connection operations that can be cancelled.
@ resolve_timeout
Resolve timeout.
@ pong_timeout
Connect timeout.
@ connect_timeout
Connect timeout.
@ run
Refers to connection::async_run operations.