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
33template <
class Runner,
class Connection,
class Logger>
35 Runner* runner_ =
nullptr;
36 Connection* conn_ =
nullptr;
38 asio::coroutine coro_{};
41 void operator()(Self& self, system::error_code ec = {}, std::size_t = 0)
43 BOOST_ASIO_CORO_REENTER (coro_)
45 runner_->hello_req_.clear();
46 if (runner_->hello_resp_.has_value())
47 runner_->hello_resp_.value().clear();
51 conn_->async_exec(runner_->hello_req_, runner_->hello_resp_, std::move(self));
52 logger_.on_hello(ec, runner_->hello_resp_);
54 if (ec || runner_->has_error_in_response() || is_cancelled(self)) {
55 logger_.trace(
"hello-op: error/canceled. Exiting ...");
57 self.complete(!!ec ? ec : asio::error::operation_aborted);
66template <
class Runner,
class Connection,
class Logger>
69 Runner* runner_ =
nullptr;
70 Connection* conn_ =
nullptr;
72 asio::coroutine coro_{};
75 runner_op(Runner* runner, Connection* conn, Logger l)
82 void operator()( Self& self
83 , std::array<std::size_t, 3> order = {}
84 , system::error_code ec0 = {}
85 , system::error_code ec1 = {}
86 , system::error_code ec2 = {}
89 BOOST_ASIO_CORO_REENTER (coro_)
92 asio::experimental::make_parallel_group(
93 [
this](
auto token) {
return runner_->async_run_all(*conn_, logger_, token); },
94 [
this](
auto token) {
return runner_->health_checker_.async_check_health(*conn_, logger_, token); },
95 [
this](
auto token) {
return runner_->async_hello(*conn_, logger_, token); }
97 asio::experimental::wait_for_all(),
100 logger_.on_runner(ec0, ec1, ec2);
102 if (is_cancelled(self)) {
103 self.complete(asio::error::operation_aborted);
112 if (order[0] == 2 && !!ec2) {
127template <
class Runner,
class Connection,
class Logger>
129 Runner* runner_ =
nullptr;
130 Connection* conn_ =
nullptr;
132 asio::coroutine coro_{};
134 template <
class Self>
135 void operator()(Self& self, system::error_code ec = {}, std::size_t = 0)
137 BOOST_ASIO_CORO_REENTER (coro_)
139 BOOST_ASIO_CORO_YIELD
140 runner_->resv_.async_resolve(std::move(self));
141 logger_.on_resolve(ec, runner_->resv_.results());
144 BOOST_ASIO_CORO_YIELD
145 runner_->ctor_.async_connect(conn_->next_layer().next_layer(), runner_->resv_.results(), std::move(self));
146 logger_.on_connect(ec, runner_->ctor_.endpoint());
149 if (conn_->use_ssl()) {
150 BOOST_ASIO_CORO_YIELD
151 runner_->hsher_.async_handshake(conn_->next_layer(), std::move(self));
152 logger_.on_ssl_handshake(ec);
156 BOOST_ASIO_CORO_YIELD
157 conn_->async_run_lean(runner_->cfg_, logger_, std::move(self));
158 BOOST_REDIS_CHECK_OP0(;)
164template <
class Executor>
167 runner(Executor ex, config cfg)
171 , health_checker_{ex}
180 health_checker_.cancel(op);
184 void set_config(config
const& cfg)
187 resv_.set_config(cfg);
188 ctor_.set_config(cfg);
189 hsher_.set_config(cfg);
190 health_checker_.set_config(cfg);
193 template <
class Connection,
class Logger,
class CompletionToken>
194 auto async_run(Connection& conn, Logger l, CompletionToken token)
196 return asio::async_compose
198 , void(system::error_code)
199 >(runner_op<runner, Connection, Logger>{
this, &conn, l}, token, conn);
202 config
const& get_config() const noexcept {
return cfg_;}
205 using resolver_type = resolver<Executor>;
206 using connector_type = connector<Executor>;
207 using handshaker_type = detail::handshaker<Executor>;
208 using health_checker_type = health_checker<Executor>;
209 using timer_type =
typename connector_type::timer_type;
211 template <
class,
class,
class>
friend struct run_all_op;
212 template <
class,
class,
class>
friend class runner_op;
213 template <
class,
class,
class>
friend struct hello_op;
215 template <
class Connection,
class Logger,
class CompletionToken>
216 auto async_run_all(Connection& conn, Logger l, CompletionToken token)
218 return asio::async_compose
220 , void(system::error_code)
221 >(run_all_op<runner, Connection, Logger>{
this, &conn, l}, token, conn);
224 template <
class Connection,
class Logger,
class CompletionToken>
225 auto async_hello(Connection& conn, Logger l, CompletionToken token)
227 return asio::async_compose
229 , void(system::error_code)
230 >(hello_op<runner, Connection, Logger>{
this, &conn, l}, token, conn);
235 if (!cfg_.username.empty() && !cfg_.password.empty() && !cfg_.clientname.empty())
236 hello_req_.push(
"HELLO",
"3",
"AUTH", cfg_.username, cfg_.password,
"SETNAME", cfg_.clientname);
237 else if (cfg_.username.empty() && cfg_.password.empty() && cfg_.clientname.empty())
238 hello_req_.push(
"HELLO",
"3");
239 else if (cfg_.clientname.empty())
240 hello_req_.push(
"HELLO",
"3",
"AUTH", cfg_.username, cfg_.password);
242 hello_req_.push(
"HELLO",
"3",
"SETNAME", cfg_.clientname);
244 if (cfg_.database_index && cfg_.database_index.value() != 0)
245 hello_req_.push(
"SELECT", cfg_.database_index.value());
248 bool has_error_in_response() const noexcept
250 if (!hello_resp_.has_value())
253 auto f = [](
auto const& e)
255 switch (e.data_type) {
258 default:
return false;
262 return std::any_of(std::cbegin(hello_resp_.value()), std::cend(hello_resp_.value()), f);
266 connector_type ctor_;
267 handshaker_type hsher_;
268 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.