Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/source/common/singleton/manager_impl.cc
Line
Count
Source
1
#include "source/common/singleton/manager_impl.h"
2
3
#include "envoy/registry/registry.h"
4
5
#include "source/common/common/assert.h"
6
#include "source/common/common/fmt.h"
7
8
namespace Envoy {
9
namespace Singleton {
10
11
42.4k
InstanceSharedPtr ManagerImpl::get(const std::string& name, SingletonFactoryCb cb, bool pin) {
12
42.4k
  ASSERT_IS_MAIN_OR_TEST_THREAD();
13
14
42.4k
  ENVOY_BUG(Registry::FactoryRegistry<Registration>::getFactory(name) != nullptr,
15
42.4k
            "invalid singleton name '" + name + "'. Make sure it is registered.");
16
17
42.4k
  auto existing_singleton = singletons_[name].lock();
18
19
42.4k
  if (existing_singleton == nullptr) {
20
30.8k
    InstanceSharedPtr singleton = cb();
21
30.8k
    singletons_[name] = singleton;
22
30.8k
    if (pin && singleton != nullptr) {
23
1.97k
      pinned_singletons_.push_back(singleton);
24
1.97k
    }
25
30.8k
    return singleton;
26
30.8k
  } else {
27
11.5k
    return existing_singleton;
28
11.5k
  }
29
42.4k
}
30
31
} // namespace Singleton
32
} // namespace Envoy