userver: userver/clients/dns/resolver.hpp Source File
Loading...
Searching...
No Matches
resolver.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/clients/dns/resolver.hpp
4/// @brief @copybrief clients::dns::Resolver
5
6#include <userver/clients/dns/common.hpp>
7#include <userver/clients/dns/config.hpp>
8#include <userver/clients/dns/exception.hpp>
9#include <userver/engine/deadline.hpp>
10#include <userver/engine/io/sockaddr.hpp>
11#include <userver/engine/task/task_processor_fwd.hpp>
12#include <userver/utils/fast_pimpl.hpp>
13#include <userver/utils/statistics/relaxed_counter.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace clients::dns {
18
19/// @ingroup userver_clients
20///
21/// @brief Caching DNS resolver implementation.
22///
23/// Usually retrieved from clients::dns::Component.
24///
25/// Combines file-based (/etc/hosts) name resolution with network-based one.
26class Resolver {
27 public:
28 struct LookupSourceCounters {
29 utils::statistics::RelaxedCounter<size_t> file{0};
30 utils::statistics::RelaxedCounter<size_t> cached{0};
31 utils::statistics::RelaxedCounter<size_t> cached_stale{0};
32 utils::statistics::RelaxedCounter<size_t> cached_failure{0};
33 utils::statistics::RelaxedCounter<size_t> network{0};
34 utils::statistics::RelaxedCounter<size_t> network_failure{0};
35 };
36
37 Resolver(engine::TaskProcessor& fs_task_processor,
38 const ResolverConfig& config);
39 Resolver(const Resolver&) = delete;
40 Resolver(Resolver&&) = delete;
41 ~Resolver();
42
43 /// Performs a domain name resolution.
44 ///
45 /// Sources are tried in the following order:
46 /// - Cached file lookup table
47 /// - Cached network resolution results
48 /// - Network name servers
49 ///
50 /// @throws clients::dns::NotResolvedException if none of the sources provide
51 /// a result within the specified deadline.
52 AddrVector Resolve(const std::string& name, engine::Deadline deadline);
53
54 /// Returns lookup source counters.
55 const LookupSourceCounters& GetLookupSourceCounters() const;
56
57 /// Forces the reload of lookup table file. Waits until the reload is done.
58 void ReloadHosts();
59
60 /// Resets the network results cache.
61 void FlushNetworkCache();
62
63 /// Removes the specified domain name from the network results cache.
64 void FlushNetworkCache(const std::string& name);
65
66 private:
67 class Impl;
68 constexpr static size_t kSize = 1728;
69 constexpr static size_t kAlignment = 16;
70 utils::FastPimpl<Impl, kSize, kAlignment> impl_;
71};
72
73} // namespace clients::dns
74
75USERVER_NAMESPACE_END