Loading...
Searching...
No Matches
runner.hpp
1/* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com)
2 *
3 * Distributed under the Boost Software License, Version 1.0. (See
4 * accompanying file LICENSE.txt)
5 */
6
7#ifndef BOOST_REDIS_RUNNER_HPP
8#define BOOST_REDIS_RUNNER_HPP
9
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/coroutine.hpp>
22#include <boost/asio/experimental/parallel_group.hpp>
23#include <boost/asio/ip/tcp.hpp>
24#include <boost/asio/steady_timer.hpp>
25#include <boost/asio/prepend.hpp>
26#include <string>
27#include <memory>
28#include <chrono>
29
30namespace boost::redis::detail
31{
32
33void push_hello(config const& cfg, request& req);
34
35template <class Runner, class Connection, class Logger>
36struct hello_op {
37 Runner* runner_ = nullptr;
38 Connection* conn_ = nullptr;
39 Logger logger_;
40 asio::coroutine coro_{};
41
42 template <class Self>
43 void operator()(Self& self, system::error_code ec = {}, std::size_t = 0)
44 {
45 BOOST_ASIO_CORO_REENTER (coro_)
46 {
47 runner_->add_hello();
48
49 BOOST_ASIO_CORO_YIELD
50 conn_->async_exec(runner_->hello_req_, runner_->hello_resp_, std::move(self));
51 logger_.on_hello(ec, runner_->hello_resp_);
52
53 if (ec || runner_->has_error_in_response() || is_cancelled(self)) {
54 logger_.trace("hello-op: error/canceled. Exiting ...");
55 conn_->cancel(operation::run);
56 self.complete(!!ec ? ec : asio::error::operation_aborted);
57 return;
58 }
59
60 self.complete({});
61 }
62 }
63};
64
65template <class Runner, class Connection, class Logger>
66class runner_op {
67private:
68 Runner* runner_ = nullptr;
69 Connection* conn_ = nullptr;
70 Logger logger_;
71 asio::coroutine coro_{};
72
73public:
74 runner_op(Runner* runner, Connection* conn, Logger l)
75 : runner_{runner}
76 , conn_{conn}
77 , logger_{l}
78 {}
79
80 template <class Self>
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 = {}
86 , std::size_t = 0)
87 {
88 BOOST_ASIO_CORO_REENTER (coro_)
89 {
90 BOOST_ASIO_CORO_YIELD
91 runner_->resv_.async_resolve(
92 asio::prepend(std::move(self), std::array<std::size_t, 3> {}));
93
94 logger_.on_resolve(ec0, runner_->resv_.results());
95
96 if (ec0 || redis::detail::is_cancelled(self)) {
97 self.complete(!!ec0 ? ec0 : asio::error::operation_aborted);
98 return;
99 }
100
101 BOOST_ASIO_CORO_YIELD
102 runner_->ctor_.async_connect(
103 conn_->next_layer().next_layer(),
104 runner_->resv_.results(),
105 asio::prepend(std::move(self), std::array<std::size_t, 3> {}));
106
107 logger_.on_connect(ec0, runner_->ctor_.endpoint());
108
109 if (ec0 || redis::detail::is_cancelled(self)) {
110 self.complete(!!ec0 ? ec0 : asio::error::operation_aborted);
111 return;
112 }
113
114 if (conn_->use_ssl()) {
115 BOOST_ASIO_CORO_YIELD
116 runner_->hsher_.async_handshake(
117 conn_->next_layer(),
118 asio::prepend(std::move(self),
119 std::array<std::size_t, 3> {}));
120
121 logger_.on_ssl_handshake(ec0);
122 if (ec0 || redis::detail::is_cancelled(self)) {
123 self.complete(!!ec0 ? ec0 : asio::error::operation_aborted);
124 return;
125 }
126 }
127
128 // Note: Oder is important here because async_run might
129 // trigger an async_write before the async_hello thereby
130 // causing authentication problems.
131 BOOST_ASIO_CORO_YIELD
132 asio::experimental::make_parallel_group(
133 [this](auto token) { return runner_->async_hello(*conn_, logger_, token); },
134 [this](auto token) { return runner_->health_checker_.async_check_health(*conn_, logger_, token); },
135 [this](auto token) { return conn_->async_run_lean(runner_->cfg_, logger_, token); }
136 ).async_wait(
137 asio::experimental::wait_for_one_error(),
138 std::move(self));
139
140 logger_.on_runner(ec0, ec1, ec2);
141
142 if (is_cancelled(self)) {
143 self.complete(asio::error::operation_aborted);
144 return;
145 }
146
147 if (order[0] == 0 && !!ec0) {
148 self.complete(ec0);
149 return;
150 }
151
152 if (order[0] == 1 && ec1 == error::pong_timeout) {
153 self.complete(ec1);
154 return;
155 }
156
157 self.complete(ec2);
158 }
159 }
160};
161
162template <class Executor>
163class runner {
164public:
165 runner(Executor ex, config cfg)
166 : resv_{ex}
167 , hsher_{ex}
168 , health_checker_{ex}
169 , cfg_{cfg}
170 { }
171
172 std::size_t cancel(operation op)
173 {
174 resv_.cancel(op);
175 hsher_.cancel(op);
176 health_checker_.cancel(op);
177 return 0U;
178 }
179
180 void set_config(config const& cfg)
181 {
182 cfg_ = cfg;
183 resv_.set_config(cfg);
184 ctor_.set_config(cfg);
185 hsher_.set_config(cfg);
186 health_checker_.set_config(cfg);
187 }
188
189 template <class Connection, class Logger, class CompletionToken>
190 auto async_run(Connection& conn, Logger l, CompletionToken token)
191 {
192 return asio::async_compose
193 < CompletionToken
194 , void(system::error_code)
195 >(runner_op<runner, Connection, Logger>{this, &conn, l}, token, conn);
196 }
197
198 config const& get_config() const noexcept {return cfg_;}
199
200private:
201 using resolver_type = resolver<Executor>;
202 using handshaker_type = detail::handshaker<Executor>;
203 using health_checker_type = health_checker<Executor>;
204
205 template <class, class, class> friend class runner_op;
206 template <class, class, class> friend struct hello_op;
207
208 template <class Connection, class Logger, class CompletionToken>
209 auto async_hello(Connection& conn, Logger l, CompletionToken token)
210 {
211 return asio::async_compose
212 < CompletionToken
213 , void(system::error_code)
214 >(hello_op<runner, Connection, Logger>{this, &conn, l}, token, conn);
215 }
216
217 void add_hello()
218 {
219 hello_req_.clear();
220 if (hello_resp_.has_value())
221 hello_resp_.value().clear();
222 push_hello(cfg_, hello_req_);
223 }
224
225 bool has_error_in_response() const noexcept
226 {
227 if (!hello_resp_.has_value())
228 return true;
229
230 auto f = [](auto const& e)
231 {
232 switch (e.data_type) {
234 case resp3::type::blob_error: return true;
235 default: return false;
236 }
237 };
238
239 return std::any_of(std::cbegin(hello_resp_.value()), std::cend(hello_resp_.value()), f);
240 }
241
242 resolver_type resv_;
243 connector ctor_;
244 handshaker_type hsher_;
245 health_checker_type health_checker_;
246 request hello_req_;
247 generic_response hello_resp_;
248 config cfg_;
249};
250
251} // boost::redis::detail
252
253#endif // BOOST_REDIS_RUNNER_HPP
adapter::result< std::vector< resp3::node > > generic_response
A generic response to a request.
Definition: response.hpp:35
operation
Connection operations that can be cancelled.
Definition: operation.hpp:18
@ pong_timeout
Connect timeout.
@ run
Refers to connection::async_run operations.