Skip to content

Commit 53cabf0

Browse files
committed
Simplifies resolve timeout operation.
1 parent c309c57 commit 53cabf0

File tree

1 file changed

+13
-53
lines changed

1 file changed

+13
-53
lines changed

include/boost/redis/detail/resolver.hpp

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
#include <boost/redis/error.hpp>
1313
#include <boost/asio/compose.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

@@ -28,66 +27,29 @@ struct resolve_op {
2827

2928
template <class Self>
3029
void operator()( Self& self
31-
, std::array<std::size_t, 2> order = {}
32-
, system::error_code ec1 = {}
33-
, asio::ip::tcp::resolver::results_type res = {}
34-
, system::error_code ec2 = {})
30+
, system::error_code ec = {}
31+
, asio::ip::tcp::resolver::results_type res = {})
3532
{
3633
BOOST_ASIO_CORO_REENTER (coro)
3734
{
38-
resv_->timer_.expires_after(resv_->timeout_);
39-
4035
BOOST_ASIO_CORO_YIELD
41-
asio::experimental::make_parallel_group(
42-
[this](auto token)
43-
{
44-
return resv_->resv_.async_resolve(resv_->addr_.host, resv_->addr_.port, token);
45-
},
46-
[this](auto token) { return resv_->timer_.async_wait(token);}
47-
).async_wait(
48-
asio::experimental::wait_for_one(),
49-
std::move(self));
50-
51-
if (is_cancelled(self)) {
52-
self.complete(asio::error::operation_aborted);
53-
return;
54-
}
55-
56-
switch (order[0]) {
57-
case 0: {
58-
// Resolver completed first.
59-
resv_->results_ = res;
60-
self.complete(ec1);
61-
} break;
62-
63-
case 1: {
64-
if (ec2) {
65-
// Timer completed first with error, perhaps a
66-
// cancellation going on.
67-
self.complete(ec2);
68-
} else {
69-
// Timer completed first without an error, this is a
70-
// resolve timeout.
71-
self.complete(error::resolve_timeout);
72-
}
73-
} break;
74-
75-
default: BOOST_ASSERT(false);
76-
}
36+
resv_->resv_.async_resolve(
37+
resv_->addr_.host,
38+
resv_->addr_.port,
39+
asio::cancel_after(resv_->timeout_, std::move(self)));
40+
41+
resv_->results_ = res;
42+
43+
// TODO: map operation_canceled into error::resolve_timeout
44+
self.complete(ec);
7745
}
7846
}
7947
};
8048

8149
template <class Executor>
8250
class resolver {
8351
public:
84-
using timer_type =
85-
asio::basic_waitable_timer<
86-
std::chrono::steady_clock,
87-
asio::wait_traits<std::chrono::steady_clock>,
88-
Executor>;
89-
90-
resolver(Executor ex) : resv_{ex} , timer_{ex} {}
52+
resolver(Executor ex) : resv_{ex} {}
9153

9254
template <class CompletionToken>
9355
auto async_resolve(CompletionToken&& token)
@@ -104,7 +66,6 @@ class resolver {
10466
case operation::resolve:
10567
case operation::all:
10668
resv_.cancel();
107-
timer_.cancel();
10869
break;
10970
default: /* ignore */;
11071
}
@@ -126,7 +87,6 @@ class resolver {
12687
template <class> friend struct resolve_op;
12788

12889
resolver_type resv_;
129-
timer_type timer_;
13090
address addr_;
13191
std::chrono::steady_clock::duration timeout_;
13292
asio::ip::tcp::resolver::results_type results_;

0 commit comments

Comments
 (0)