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 {
27public:
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, const ResolverConfig& config);
38 Resolver(const Resolver&) = delete;
39 Resolver(Resolver&&) = delete;
40 ~Resolver();
41
42 /// Performs a domain name resolution.
43 ///
44 /// Sources are tried in the following order:
45 /// - Cached file lookup table
46 /// - Cached network resolution results
47 /// - Network name servers
48 ///
49 /// @throws clients::dns::NotResolvedException if none of the sources provide
50 /// a result within the specified deadline.
51 AddrVector Resolve(const std::string& name, engine::Deadline deadline);
52
53 /// Returns lookup source counters.
54 const LookupSourceCounters& GetLookupSourceCounters() const;
55
56 /// Forces the reload of lookup table file. Waits until the reload is done.
57 void ReloadHosts();
58
59 /// Resets the network results cache.
60 void FlushNetworkCache();
61
62 /// Removes the specified domain name from the network results cache.
63 void FlushNetworkCache(const std::string& name);
64
65private:
66 class Impl;
67 constexpr static size_t kSize = 1728;
68 constexpr static size_t kAlignment = 16;
69 utils::FastPimpl<Impl, kSize, kAlignment> impl_;
70};
71
72} // namespace clients::dns
73
74USERVER_NAMESPACE_END