Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/api/api.h" 4 : #include "envoy/config/bootstrap/v3/bootstrap.pb.h" 5 : #include "envoy/config/core/v3/grpc_service.pb.h" 6 : #include "envoy/grpc/async_client_manager.h" 7 : #include "envoy/singleton/manager.h" 8 : #include "envoy/stats/scope.h" 9 : #include "envoy/thread_local/thread_local.h" 10 : #include "envoy/upstream/cluster_manager.h" 11 : 12 : #include "source/common/grpc/stat_names.h" 13 : #include "source/common/protobuf/utility.h" 14 : 15 : namespace Envoy { 16 : namespace Grpc { 17 : 18 : class AsyncClientFactoryImpl : public AsyncClientFactory { 19 : public: 20 : AsyncClientFactoryImpl(Upstream::ClusterManager& cm, 21 : const envoy::config::core::v3::GrpcService& config, 22 : bool skip_cluster_check, TimeSource& time_source); 23 : RawAsyncClientPtr createUncachedRawAsyncClient() override; 24 : 25 : private: 26 : Upstream::ClusterManager& cm_; 27 : const envoy::config::core::v3::GrpcService config_; 28 : TimeSource& time_source_; 29 : }; 30 : 31 : class GoogleAsyncClientFactoryImpl : public AsyncClientFactory { 32 : public: 33 : GoogleAsyncClientFactoryImpl(ThreadLocal::Instance& tls, ThreadLocal::Slot* google_tls_slot, 34 : Stats::Scope& scope, 35 : const envoy::config::core::v3::GrpcService& config, Api::Api& api, 36 : const StatNames& stat_names); 37 : RawAsyncClientPtr createUncachedRawAsyncClient() override; 38 : 39 : private: 40 : ThreadLocal::Instance& tls_; 41 : ThreadLocal::Slot* google_tls_slot_; 42 : Stats::ScopeSharedPtr scope_; 43 : const envoy::config::core::v3::GrpcService config_; 44 : Api::Api& api_; 45 : const StatNames& stat_names_; 46 : }; 47 : 48 : class AsyncClientManagerImpl : public AsyncClientManager { 49 : public: 50 : AsyncClientManagerImpl( 51 : Upstream::ClusterManager& cm, ThreadLocal::Instance& tls, TimeSource& time_source, 52 : Api::Api& api, const StatNames& stat_names, 53 : const envoy::config::bootstrap::v3::Bootstrap::GrpcAsyncClientManagerConfig& config); 54 : RawAsyncClientSharedPtr 55 : getOrCreateRawAsyncClient(const envoy::config::core::v3::GrpcService& config, Stats::Scope& scope, 56 : bool skip_cluster_check) override; 57 : 58 : RawAsyncClientSharedPtr 59 : getOrCreateRawAsyncClientWithHashKey(const GrpcServiceConfigWithHashKey& config_with_hash_key, 60 : Stats::Scope& scope, bool skip_cluster_check) override; 61 : 62 : AsyncClientFactoryPtr factoryForGrpcService(const envoy::config::core::v3::GrpcService& config, 63 : Stats::Scope& scope, 64 : bool skip_cluster_check) override; 65 : class RawAsyncClientCache : public ThreadLocal::ThreadLocalObject { 66 : public: 67 : explicit RawAsyncClientCache(Event::Dispatcher& dispatcher, 68 : std::chrono::milliseconds max_cached_entry_idle_duration); 69 : void setCache(const GrpcServiceConfigWithHashKey& config_with_hash_key, 70 : const RawAsyncClientSharedPtr& client); 71 : 72 : RawAsyncClientSharedPtr getCache(const GrpcServiceConfigWithHashKey& config_with_hash_key); 73 : 74 : private: 75 : void evictEntriesAndResetEvictionTimer(); 76 : struct CacheEntry { 77 : CacheEntry(const GrpcServiceConfigWithHashKey& config_with_hash_key, 78 : RawAsyncClientSharedPtr const& client, MonotonicTime create_time) 79 : : config_with_hash_key_(config_with_hash_key), client_(client), 80 5 : accessed_time_(create_time) {} 81 : GrpcServiceConfigWithHashKey config_with_hash_key_; 82 : RawAsyncClientSharedPtr client_; 83 : MonotonicTime accessed_time_; 84 : }; 85 : using LruList = std::list<CacheEntry>; 86 : LruList lru_list_; 87 : absl::flat_hash_map<GrpcServiceConfigWithHashKey, LruList::iterator> lru_map_; 88 : Event::Dispatcher& dispatcher_; 89 : Envoy::Event::TimerPtr cache_eviction_timer_; 90 : const std::chrono::milliseconds max_cached_entry_idle_duration_; 91 : }; 92 : 93 : private: 94 : Upstream::ClusterManager& cm_; 95 : ThreadLocal::Instance& tls_; 96 : ThreadLocal::SlotPtr google_tls_slot_; 97 : TimeSource& time_source_; 98 : Api::Api& api_; 99 : const StatNames& stat_names_; 100 : ThreadLocal::TypedSlot<RawAsyncClientCache> raw_async_client_cache_; 101 : }; 102 : 103 : } // namespace Grpc 104 : } // namespace Envoy