Boost.Redis: include/boost/redis/detail/resolver.hpp Source File

Boost.Redis 1.84.0 A redis client library

Loading...
Searching...
No Matches
resolver.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_RESOLVER_HPP
8#define BOOST_REDIS_RESOLVER_HPP
9
10#include <boost/redis/config.hpp>
11#include <boost/redis/error.hpp>
12#include <boost/asio/compose.hpp>
13#include <boost/asio/coroutine.hpp>
14#include <boost/asio/ip/tcp.hpp>
15#include <boost/asio/cancel_after.hpp>
16#include <string>
17#include <chrono>
18
19namespace boost::redis::detail
20{
21
22template <class Resolver>
23struct resolve_op {
24 Resolver* resv_ = nullptr;
25 asio::coroutine coro{};
26
27 template <class Self>
28 void operator()( Self& self
29 , system::error_code ec = {}
30 , asio::ip::tcp::resolver::results_type res = {})
31 {
32 BOOST_ASIO_CORO_REENTER (coro)
33 {
34 BOOST_ASIO_CORO_YIELD
35 resv_->resv_.async_resolve(
36 resv_->addr_.host,
37 resv_->addr_.port,
38 asio::cancel_after(resv_->timeout_, std::move(self)));
39
40 resv_->results_ = res;
41
42 if (ec == asio::error::operation_aborted) {
43 self.complete(error::resolve_timeout);
44 } else {
45 self.complete(ec);
46 }
47 }
48 }
49};
50
51template <class Executor>
52class resolver {
53public:
54 resolver(Executor ex) : resv_{ex} {}
55
56 template <class CompletionToken>
57 auto async_resolve(CompletionToken&& token)
58 {
59 return asio::async_compose
60 < CompletionToken
61 , void(system::error_code)
62 >(resolve_op<resolver>{this}, token, resv_);
63 }
64
65 void cancel()
66 { resv_.cancel(); }
67
68 auto const& results() const noexcept
69 { return results_;}
70
71 void set_config(config const& cfg)
72 {
73 addr_ = cfg.addr;
74 timeout_ = cfg.resolve_timeout;
75 }
76
77private:
78 using resolver_type = asio::ip::basic_resolver<asio::ip::tcp, Executor>;
79 template <class> friend struct resolve_op;
80
81 resolver_type resv_;
82 address addr_;
83 std::chrono::steady_clock::duration timeout_;
84 asio::ip::tcp::resolver::results_type results_;
85};
86
87} // boost::redis::detail
88
89#endif // BOOST_REDIS_RESOLVER_HPP
boost::redis::error::resolve_timeout
@ resolve_timeout
Resolve timeout.