resolver.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Copyright (c) 2018-2023 Marcelo Zimbres Silva ([email protected])
  2. *
  3. * Distributed under the Boost Software License, Version 1.0. (See
  4. * accompanying file LICENSE.txt)
  5. */
  6. #ifndef BOOST_REDIS_RESOLVER_HPP
  7. #define BOOST_REDIS_RESOLVER_HPP
  8. #include <boost/redis/config.hpp>
  9. #include <boost/redis/detail/helper.hpp>
  10. #include <boost/redis/error.hpp>
  11. #include <boost/asio/compose.hpp>
  12. #include <boost/asio/coroutine.hpp>
  13. #include <boost/asio/experimental/parallel_group.hpp>
  14. #include <boost/asio/ip/tcp.hpp>
  15. #include <boost/asio/steady_timer.hpp>
  16. #include <string>
  17. #include <chrono>
  18. namespace boost::redis::detail
  19. {
  20. template <class Resolver>
  21. struct resolve_op {
  22. Resolver* resv_ = nullptr;
  23. asio::coroutine coro{};
  24. template <class Self>
  25. void operator()( Self& self
  26. , std::array<std::size_t, 2> order = {}
  27. , system::error_code ec1 = {}
  28. , asio::ip::tcp::resolver::results_type res = {}
  29. , system::error_code ec2 = {})
  30. {
  31. BOOST_ASIO_CORO_REENTER (coro)
  32. {
  33. resv_->timer_.expires_after(resv_->timeout_);
  34. BOOST_ASIO_CORO_YIELD
  35. asio::experimental::make_parallel_group(
  36. [this](auto token)
  37. {
  38. return resv_->resv_.async_resolve(resv_->addr_.host, resv_->addr_.port, token);
  39. },
  40. [this](auto token) { return resv_->timer_.async_wait(token);}
  41. ).async_wait(
  42. asio::experimental::wait_for_one(),
  43. std::move(self));
  44. if (is_cancelled(self)) {
  45. self.complete(asio::error::operation_aborted);
  46. return;
  47. }
  48. switch (order[0]) {
  49. case 0: {
  50. // Resolver completed first.
  51. resv_->results_ = res;
  52. self.complete(ec1);
  53. } break;
  54. case 1: {
  55. if (ec2) {
  56. // Timer completed first with error, perhaps a
  57. // cancellation going on.
  58. self.complete(ec2);
  59. } else {
  60. // Timer completed first without an error, this is a
  61. // resolve timeout.
  62. self.complete(error::resolve_timeout);
  63. }
  64. } break;
  65. default: BOOST_ASSERT(false);
  66. }
  67. }
  68. }
  69. };
  70. template <class Executor>
  71. class resolver {
  72. public:
  73. using timer_type =
  74. asio::basic_waitable_timer<
  75. std::chrono::steady_clock,
  76. asio::wait_traits<std::chrono::steady_clock>,
  77. Executor>;
  78. resolver(Executor ex) : resv_{ex} , timer_{ex} {}
  79. template <class CompletionToken>
  80. auto async_resolve(CompletionToken&& token)
  81. {
  82. return asio::async_compose
  83. < CompletionToken
  84. , void(system::error_code)
  85. >(resolve_op<resolver>{this}, token, resv_);
  86. }
  87. std::size_t cancel(operation op)
  88. {
  89. switch (op) {
  90. case operation::resolve:
  91. case operation::all:
  92. resv_.cancel();
  93. timer_.cancel();
  94. break;
  95. default: /* ignore */;
  96. }
  97. return 0;
  98. }
  99. auto const& results() const noexcept
  100. { return results_;}
  101. void set_config(config const& cfg)
  102. {
  103. addr_ = cfg.addr;
  104. timeout_ = cfg.resolve_timeout;
  105. }
  106. private:
  107. using resolver_type = asio::ip::basic_resolver<asio::ip::tcp, Executor>;
  108. template <class> friend struct resolve_op;
  109. resolver_type resv_;
  110. timer_type timer_;
  111. address addr_;
  112. std::chrono::steady_clock::duration timeout_;
  113. asio::ip::tcp::resolver::results_type results_;
  114. };
  115. } // boost::redis::detail
  116. #endif // BOOST_REDIS_RESOLVER_HPP