Skip to content

Commit 943cf0d

Browse files
committed
Simplifies the connect operations.
1 parent 53cabf0 commit 943cf0d

File tree

3 files changed

+16
-70
lines changed

3 files changed

+16
-70
lines changed

include/boost/redis/detail/connector.hpp

+12-63
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
#include <boost/asio/compose.hpp>
1313
#include <boost/asio/connect.hpp>
1414
#include <boost/asio/coroutine.hpp>
15-
#include <boost/asio/experimental/parallel_group.hpp>
1615
#include <boost/asio/ip/tcp.hpp>
17-
#include <boost/asio/steady_timer.hpp>
16+
#include <boost/asio/cancel_after.hpp>
1817
#include <string>
1918
#include <chrono>
2019

@@ -30,65 +29,29 @@ struct connect_op {
3029

3130
template <class Self>
3231
void operator()( Self& self
33-
, std::array<std::size_t, 2> const& order = {}
34-
, system::error_code const& ec1 = {}
35-
, asio::ip::tcp::endpoint const& ep= {}
36-
, system::error_code const& ec2 = {})
32+
, system::error_code const& ec = {}
33+
, asio::ip::tcp::endpoint const& ep= {})
3734
{
3835
BOOST_ASIO_CORO_REENTER (coro)
3936
{
40-
ctor_->timer_.expires_after(ctor_->timeout_);
41-
4237
BOOST_ASIO_CORO_YIELD
43-
asio::experimental::make_parallel_group(
44-
[this](auto token)
45-
{
46-
auto f = [](system::error_code const&, auto const&) { return true; };
47-
return asio::async_connect(*stream, *res_, f, token);
48-
},
49-
[this](auto token) { return ctor_->timer_.async_wait(token);}
50-
).async_wait(
51-
asio::experimental::wait_for_one(),
52-
std::move(self));
53-
54-
if (is_cancelled(self)) {
55-
self.complete(asio::error::operation_aborted);
56-
return;
57-
}
38+
asio::async_connect(*stream, *res_,
39+
[](system::error_code const&, auto const&) { return true; },
40+
asio::cancel_after(ctor_->timeout_, std::move(self)));
5841

59-
switch (order[0]) {
60-
case 0: {
61-
ctor_->endpoint_ = ep;
62-
self.complete(ec1);
63-
} break;
64-
case 1:
65-
{
66-
if (ec2) {
67-
self.complete(ec2);
68-
} else {
69-
self.complete(error::connect_timeout);
70-
}
71-
} break;
42+
ctor_->endpoint_ = ep;
7243

73-
default: BOOST_ASSERT(false);
44+
if (ec == asio::error::operation_aborted) {
45+
ec == error::connect_timeout;
7446
}
47+
48+
self.complete(ec);
7549
}
7650
}
7751
};
7852

79-
template <class Executor>
8053
class connector {
8154
public:
82-
using timer_type =
83-
asio::basic_waitable_timer<
84-
std::chrono::steady_clock,
85-
asio::wait_traits<std::chrono::steady_clock>,
86-
Executor>;
87-
88-
connector(Executor ex)
89-
: timer_{ex}
90-
{}
91-
9255
void set_config(config const& cfg)
9356
{ timeout_ = cfg.connect_timeout; }
9457

@@ -102,28 +65,14 @@ class connector {
10265
return asio::async_compose
10366
< CompletionToken
10467
, void(system::error_code)
105-
>(connect_op<connector, Stream>{this, &stream, &res}, token, timer_);
106-
}
107-
108-
std::size_t cancel(operation op)
109-
{
110-
switch (op) {
111-
case operation::connect:
112-
case operation::all:
113-
timer_.cancel();
114-
break;
115-
default: /* ignore */;
116-
}
117-
118-
return 0;
68+
>(connect_op<connector, Stream>{this, &stream, &res}, token);
11969
}
12070

12171
auto const& endpoint() const noexcept { return endpoint_;}
12272

12373
private:
12474
template <class, class> friend struct connect_op;
12575

126-
timer_type timer_;
12776
std::chrono::steady_clock::duration timeout_ = std::chrono::seconds{2};
12877
asio::ip::tcp::endpoint endpoint_;
12978
};

include/boost/redis/detail/resolver.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ struct resolve_op {
4040

4141
resv_->results_ = res;
4242

43-
// TODO: map operation_canceled into error::resolve_timeout
43+
if (ec == asio::error::operation_aborted) {
44+
ec == error::resolve_timeout;
45+
}
4446
self.complete(ec);
4547
}
4648
}

include/boost/redis/detail/runner.hpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <boost/redis/detail/resolver.hpp>
1919
#include <boost/redis/detail/handshaker.hpp>
2020
#include <boost/asio/compose.hpp>
21-
#include <boost/asio/connect.hpp>
2221
#include <boost/asio/coroutine.hpp>
2322
#include <boost/asio/experimental/parallel_group.hpp>
2423
#include <boost/asio/ip/tcp.hpp>
@@ -165,7 +164,6 @@ class runner {
165164
public:
166165
runner(Executor ex, config cfg)
167166
: resv_{ex}
168-
, ctor_{ex}
169167
, hsher_{ex}
170168
, health_checker_{ex}
171169
, cfg_{cfg}
@@ -174,7 +172,6 @@ class runner {
174172
std::size_t cancel(operation op)
175173
{
176174
resv_.cancel(op);
177-
ctor_.cancel(op);
178175
hsher_.cancel(op);
179176
health_checker_.cancel(op);
180177
return 0U;
@@ -202,10 +199,8 @@ class runner {
202199

203200
private:
204201
using resolver_type = resolver<Executor>;
205-
using connector_type = connector<Executor>;
206202
using handshaker_type = detail::handshaker<Executor>;
207203
using health_checker_type = health_checker<Executor>;
208-
using timer_type = typename connector_type::timer_type;
209204

210205
template <class, class, class> friend class runner_op;
211206
template <class, class, class> friend struct hello_op;
@@ -245,7 +240,7 @@ class runner {
245240
}
246241

247242
resolver_type resv_;
248-
connector_type ctor_;
243+
connector ctor_;
249244
handshaker_type hsher_;
250245
health_checker_type health_checker_;
251246
request hello_req_;

0 commit comments

Comments
 (0)