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
125072
InstanceSharedPtr ManagerImpl::get(const std::string& name, SingletonFactoryCb cb, bool pin) {
12
125072
  ASSERT_IS_MAIN_OR_TEST_THREAD();
13

            
14
125072
  ENVOY_BUG(Registry::FactoryRegistry<Registration>::getFactory(name) != nullptr,
15
125072
            "invalid singleton name '" + name + "'. Make sure it is registered.");
16

            
17
125072
  auto existing_singleton = singletons_[name].lock();
18

            
19
125072
  if (existing_singleton == nullptr) {
20
96532
    InstanceSharedPtr singleton = cb();
21
96532
    singletons_[name] = singleton;
22
96532
    if (pin && singleton != nullptr) {
23
21164
      pinned_singletons_.push_back(singleton);
24
21164
    }
25
96532
    return singleton;
26
103725
  } else {
27
28540
    return existing_singleton;
28
28540
  }
29
125072
}
30

            
31
} // namespace Singleton
32
} // namespace Envoy