Line data Source code
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 1381 : InstanceSharedPtr ManagerImpl::get(const std::string& name, SingletonFactoryCb cb, bool pin) { 12 1381 : ASSERT(run_tid_ == thread_factory_.currentThreadId()); 13 : 14 1381 : ENVOY_BUG(Registry::FactoryRegistry<Registration>::getFactory(name) != nullptr, 15 1381 : "invalid singleton name '" + name + "'. Make sure it is registered."); 16 : 17 1381 : auto existing_singleton = singletons_[name].lock(); 18 : 19 1381 : if (existing_singleton == nullptr) { 20 976 : InstanceSharedPtr singleton = cb(); 21 976 : singletons_[name] = singleton; 22 976 : if (pin && singleton != nullptr) { 23 95 : pinned_singletons_.push_back(singleton); 24 95 : } 25 976 : return singleton; 26 1122 : } else { 27 405 : return existing_singleton; 28 405 : } 29 1381 : } 30 : 31 : } // namespace Singleton 32 : } // namespace Envoy