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

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

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

            
19
124821
  if (existing_singleton == nullptr) {
20
96409
    InstanceSharedPtr singleton = cb();
21
96409
    singletons_[name] = singleton;
22
96409
    if (pin && singleton != nullptr) {
23
21136
      pinned_singletons_.push_back(singleton);
24
21136
    }
25
96409
    return singleton;
26
103570
  } else {
27
28412
    return existing_singleton;
28
28412
  }
29
124821
}
30

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