1
#include "source/extensions/common/dynamic_forward_proxy/dns_cache_manager_impl.h"
2

            
3
#include "envoy/extensions/common/dynamic_forward_proxy/v3/dns_cache.pb.h"
4

            
5
#include "source/common/protobuf/protobuf.h"
6
#include "source/extensions/common/dynamic_forward_proxy/dns_cache_impl.h"
7

            
8
#include "absl/container/flat_hash_map.h"
9

            
10
namespace Envoy {
11
namespace Extensions {
12
namespace Common {
13
namespace DynamicForwardProxy {
14

            
15
SINGLETON_MANAGER_REGISTRATION(dns_cache_manager);
16

            
17
absl::StatusOr<DnsCacheSharedPtr> DnsCacheManagerImpl::getCache(
18
150
    const envoy::extensions::common::dynamic_forward_proxy::v3::DnsCacheConfig& config) {
19
150
  const auto& existing_cache = caches_.find(config.name());
20
150
  if (existing_cache != caches_.end()) {
21
38
    if (!Protobuf::util::MessageDifferencer::Equivalent(config, existing_cache->second.config_)) {
22
1
      return absl::InvalidArgumentError(
23
1
          fmt::format("config specified DNS cache '{}' with different settings", config.name()));
24
1
    }
25

            
26
37
    return existing_cache->second.cache_;
27
38
  }
28

            
29
112
  auto cache_or_status = DnsCacheImpl::createDnsCacheImpl(context_, config);
30
112
  RETURN_IF_NOT_OK_REF(cache_or_status.status());
31
112
  DnsCacheSharedPtr new_cache = std::move(cache_or_status.value());
32
112
  caches_.emplace(config.name(), ActiveCache{config, new_cache});
33
112
  return new_cache;
34
112
}
35

            
36
2
DnsCacheSharedPtr DnsCacheManagerImpl::lookUpCacheByName(absl::string_view cache_name) {
37
2
  ASSERT(context_.serverFactoryContext().mainThreadDispatcher().isThreadSafe());
38
2
  const auto& existing_cache = caches_.find(cache_name);
39
2
  if (existing_cache != caches_.end()) {
40
1
    return existing_cache->second.cache_;
41
1
  }
42

            
43
1
  return nullptr;
44
2
}
45

            
46
208
DnsCacheManagerSharedPtr DnsCacheManagerFactoryImpl::get() {
47
208
  return context_.serverFactoryContext().singletonManager().getTyped<DnsCacheManager>(
48
208
      SINGLETON_MANAGER_REGISTERED_NAME(dns_cache_manager),
49
208
      [this] { return std::make_shared<DnsCacheManagerImpl>(context_); });
50
208
}
51

            
52
} // namespace DynamicForwardProxy
53
} // namespace Common
54
} // namespace Extensions
55
} // namespace Envoy