Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/config/core/v3/protocol.pb.h" 4 : #include "envoy/http/http_server_properties_cache.h" 5 : #include "envoy/server/factory_context.h" 6 : #include "envoy/singleton/instance.h" 7 : #include "envoy/singleton/manager.h" 8 : #include "envoy/thread_local/thread_local.h" 9 : 10 : #include "absl/container/flat_hash_map.h" 11 : 12 : namespace Envoy { 13 : namespace Http { 14 : 15 : struct AlternateProtocolsData { 16 : AlternateProtocolsData(Server::Configuration::ServerFactoryContext& context, 17 : ProtobufMessage::ValidationVisitor& validation_visitor) 18 : : dispatcher_(context.mainThreadDispatcher()), validation_visitor_(validation_visitor), 19 131 : file_system_(context.api().fileSystem()), concurrency_(context.options().concurrency()) {} 20 : 21 : Event::Dispatcher& dispatcher_; 22 : ProtobufMessage::ValidationVisitor& validation_visitor_; 23 : Filesystem::Instance& file_system_; 24 : uint32_t concurrency_; 25 : }; 26 : 27 : class HttpServerPropertiesCacheManagerImpl : public HttpServerPropertiesCacheManager, 28 : public Singleton::Instance { 29 : public: 30 : HttpServerPropertiesCacheManagerImpl(AlternateProtocolsData& data, 31 : ThreadLocal::SlotAllocator& tls); 32 : 33 : // HttpServerPropertiesCacheManager 34 : HttpServerPropertiesCacheSharedPtr 35 : getCache(const envoy::config::core::v3::AlternateProtocolsCacheOptions& options, 36 : Event::Dispatcher& dispatcher) override; 37 : 38 : private: 39 : // Contains a cache and the options associated with it. 40 : struct CacheWithOptions { 41 : CacheWithOptions(const envoy::config::core::v3::AlternateProtocolsCacheOptions& options, 42 : HttpServerPropertiesCacheSharedPtr cache) 43 48 : : options_(options), cache_(cache) {} 44 : 45 : const envoy::config::core::v3::AlternateProtocolsCacheOptions options_; 46 : HttpServerPropertiesCacheSharedPtr cache_; 47 : }; 48 : 49 : // Per-thread state. 50 : struct State : public ThreadLocal::ThreadLocalObject { 51 : // Map from config name to cache for that config. 52 : absl::flat_hash_map<std::string, CacheWithOptions> caches_; 53 : }; 54 : 55 : AlternateProtocolsData& data_; 56 : 57 : // Thread local state for the cache. 58 : ThreadLocal::TypedSlot<State> slot_; 59 : }; 60 : 61 : class HttpServerPropertiesCacheManagerFactoryImpl : public HttpServerPropertiesCacheManagerFactory { 62 : public: 63 : HttpServerPropertiesCacheManagerFactoryImpl(Singleton::Manager& singleton_manager, 64 : ThreadLocal::SlotAllocator& tls, 65 : AlternateProtocolsData data) 66 131 : : singleton_manager_(singleton_manager), tls_(tls), data_(data) {} 67 : 68 : HttpServerPropertiesCacheManagerSharedPtr get() override; 69 : 70 : private: 71 : Singleton::Manager& singleton_manager_; 72 : ThreadLocal::SlotAllocator& tls_; 73 : AlternateProtocolsData data_; 74 : }; 75 : 76 : } // namespace Http 77 : } // namespace Envoy