Skip to content

Commit c309c57

Browse files
committed
Disentangles resolve and connect from run operations.
1 parent a535862 commit c309c57

File tree

5 files changed

+62
-72
lines changed

5 files changed

+62
-72
lines changed

include/boost/redis/detail/connection_base.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018-2023 Marcelo Zimbres Silva ([email protected])
1+
/* Copyright (c) 2018-2024 Marcelo Zimbres Silva ([email protected])
22
*
33
* Distributed under the Boost Software License, Version 1.0. (See
44
* accompanying file LICENSE.txt)
@@ -705,7 +705,7 @@ class connection_base {
705705
template <class, class> friend struct writer_op;
706706
template <class, class> friend struct run_op;
707707
template <class> friend struct exec_op;
708-
template <class, class, class> friend struct run_all_op;
708+
template <class, class, class> friend struct runner_op;
709709

710710
void cancel_push_requests()
711711
{

include/boost/redis/detail/runner.hpp

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018-2023 Marcelo Zimbres Silva ([email protected])
1+
/* Copyright (c) 2018-2024 Marcelo Zimbres Silva ([email protected])
22
*
33
* Distributed under the Boost Software License, Version 1.0. (See
44
* accompanying file LICENSE.txt)
@@ -23,6 +23,7 @@
2323
#include <boost/asio/experimental/parallel_group.hpp>
2424
#include <boost/asio/ip/tcp.hpp>
2525
#include <boost/asio/steady_timer.hpp>
26+
#include <boost/asio/prepend.hpp>
2627
#include <string>
2728
#include <memory>
2829
#include <chrono>
@@ -87,11 +88,52 @@ class runner_op {
8788
{
8889
BOOST_ASIO_CORO_REENTER (coro_)
8990
{
91+
BOOST_ASIO_CORO_YIELD
92+
runner_->resv_.async_resolve(
93+
asio::prepend(std::move(self), std::array<std::size_t, 3> {}));
94+
95+
logger_.on_resolve(ec0, runner_->resv_.results());
96+
97+
if (ec0 || redis::detail::is_cancelled(self)) {
98+
self.complete(!!ec0 ? ec0 : asio::error::operation_aborted);
99+
return;
100+
}
101+
102+
BOOST_ASIO_CORO_YIELD
103+
runner_->ctor_.async_connect(
104+
conn_->next_layer().next_layer(),
105+
runner_->resv_.results(),
106+
asio::prepend(std::move(self), std::array<std::size_t, 3> {}));
107+
108+
logger_.on_connect(ec0, runner_->ctor_.endpoint());
109+
110+
if (ec0 || redis::detail::is_cancelled(self)) {
111+
self.complete(!!ec0 ? ec0 : asio::error::operation_aborted);
112+
return;
113+
}
114+
115+
if (conn_->use_ssl()) {
116+
BOOST_ASIO_CORO_YIELD
117+
runner_->hsher_.async_handshake(
118+
conn_->next_layer(),
119+
asio::prepend(std::move(self),
120+
std::array<std::size_t, 3> {}));
121+
122+
logger_.on_ssl_handshake(ec0);
123+
if (ec0 || redis::detail::is_cancelled(self)) {
124+
self.complete(!!ec0 ? ec0 : asio::error::operation_aborted);
125+
return;
126+
}
127+
}
128+
129+
// Note: Oder is important here because async_run might
130+
// trigger an async_write before the async_hello thereby
131+
// causing authentication problems.
90132
BOOST_ASIO_CORO_YIELD
91133
asio::experimental::make_parallel_group(
92-
[this](auto token) { return runner_->async_run_all(*conn_, logger_, token); },
134+
[this](auto token) { return runner_->async_hello(*conn_, logger_, token); },
93135
[this](auto token) { return runner_->health_checker_.async_check_health(*conn_, logger_, token); },
94-
[this](auto token) { return runner_->async_hello(*conn_, logger_, token); }
136+
[this](auto token) { return conn_->async_run_lean(runner_->cfg_, logger_, token); }
95137
).async_wait(
96138
asio::experimental::wait_for_one_error(),
97139
std::move(self));
@@ -103,59 +145,17 @@ class runner_op {
103145
return;
104146
}
105147

106-
if (ec0 == error::connect_timeout || ec0 == error::resolve_timeout) {
148+
if (order[0] == 0 && !!ec0) {
107149
self.complete(ec0);
108150
return;
109151
}
110152

111-
if (order[0] == 2 && !!ec2) {
112-
self.complete(ec2);
113-
return;
114-
}
115-
116153
if (order[0] == 1 && ec1 == error::pong_timeout) {
117154
self.complete(ec1);
118155
return;
119156
}
120157

121-
self.complete(ec0);
122-
}
123-
}
124-
};
125-
126-
template <class Runner, class Connection, class Logger>
127-
struct run_all_op {
128-
Runner* runner_ = nullptr;
129-
Connection* conn_ = nullptr;
130-
Logger logger_;
131-
asio::coroutine coro_{};
132-
133-
template <class Self>
134-
void operator()(Self& self, system::error_code ec = {}, std::size_t = 0)
135-
{
136-
BOOST_ASIO_CORO_REENTER (coro_)
137-
{
138-
BOOST_ASIO_CORO_YIELD
139-
runner_->resv_.async_resolve(std::move(self));
140-
logger_.on_resolve(ec, runner_->resv_.results());
141-
BOOST_REDIS_CHECK_OP0(conn_->cancel(operation::run);)
142-
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());
146-
BOOST_REDIS_CHECK_OP0(conn_->cancel(operation::run);)
147-
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);
152-
BOOST_REDIS_CHECK_OP0(conn_->cancel(operation::run);)
153-
}
154-
155-
BOOST_ASIO_CORO_YIELD
156-
conn_->async_run_lean(runner_->cfg_, logger_, std::move(self));
157-
BOOST_REDIS_CHECK_OP0(;)
158-
self.complete(ec);
158+
self.complete(ec2);
159159
}
160160
}
161161
};
@@ -207,19 +207,9 @@ class runner {
207207
using health_checker_type = health_checker<Executor>;
208208
using timer_type = typename connector_type::timer_type;
209209

210-
template <class, class, class> friend struct run_all_op;
211210
template <class, class, class> friend class runner_op;
212211
template <class, class, class> friend struct hello_op;
213212

214-
template <class Connection, class Logger, class CompletionToken>
215-
auto async_run_all(Connection& conn, Logger l, CompletionToken token)
216-
{
217-
return asio::async_compose
218-
< CompletionToken
219-
, void(system::error_code)
220-
>(run_all_op<runner, Connection, Logger>{this, &conn, l}, token, conn);
221-
}
222-
223213
template <class Connection, class Logger, class CompletionToken>
224214
auto async_hello(Connection& conn, Logger l, CompletionToken token)
225215
{

include/boost/redis/impl/logger.ipp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,19 @@ logger::on_hello(
156156

157157
void
158158
logger::on_runner(
159-
system::error_code const& run_all_ec,
160-
system::error_code const& health_check_ec,
161-
system::error_code const& hello_ec)
159+
system::error_code const& hello,
160+
system::error_code const& check_health,
161+
system::error_code const& run)
162162
{
163163
if (level_ < level::info)
164164
return;
165165

166166
write_prefix();
167167

168-
std::clog << "runner-op: "
169-
<< run_all_ec.message() << " (async_run_all), "
170-
<< health_check_ec.message() << " (async_health_check) "
171-
<< hello_ec.message() << " (async_hello).";
168+
std::clog << "hello: "
169+
<< hello.message() << " (async_hello), "
170+
<< check_health.message() << " (async_check_health) "
171+
<< run.message() << " (async_run_lean).";
172172

173173
std::clog << std::endl;
174174
}

include/boost/redis/logger.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,15 @@ class logger {
143143
/** @brief Called when the runner operation completes.
144144
* @ingroup high-level-api
145145
*
146-
* @param run_all_ec Error code returned by the run_all operation.
147-
* @param health_check_ec Error code returned by the health checker operation.
148146
* @param hello_ec Error code returned by the health checker operation.
147+
* @param health_check_ec Error code returned by the health checker operation.
148+
* @param run_all_ec Error code returned by the run_all operation.
149149
*/
150150
void
151151
on_runner(
152-
system::error_code const& run_all_ec,
153-
system::error_code const& health_check_ec,
154-
system::error_code const& hello_ec);
152+
system::error_code const& hello,
153+
system::error_code const& health_check,
154+
system::error_code const& run);
155155

156156
void
157157
on_check_health(

test/test_conn_push.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ BOOST_AUTO_TEST_CASE(test_push_adapter)
218218

219219
auto cfg = make_test_config();
220220
conn->async_run(cfg, {}, [](auto ec){
221-
BOOST_CHECK_EQUAL(ec, boost::redis::error::incompatible_size);
221+
BOOST_CHECK_EQUAL(ec, redis::error::incompatible_size);
222222
});
223223

224224
ioc.run();

0 commit comments

Comments
 (0)