LCOV - code coverage report
Current view: top level - envoy/grpc - async_client_manager.h (source / functions) Hit Total Coverage
Test: coverage.dat Lines: 12 18 66.7 %
Date: 2024-01-05 06:35:25 Functions: 6 8 75.0 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include "envoy/config/core/v3/grpc_service.pb.h"
       4             : #include "envoy/grpc/async_client.h"
       5             : #include "envoy/stats/scope.h"
       6             : 
       7             : namespace Envoy {
       8             : namespace Grpc {
       9             : 
      10             : class AsyncClientFactoryImpl;
      11             : 
      12             : // Per-service factory for Grpc::RawAsyncClients. This factory is thread aware and will instantiate
      13             : // with thread local state. Clients will use ThreadLocal::Instance::dispatcher() for event handling.
      14             : class AsyncClientFactory {
      15             : public:
      16          36 :   virtual ~AsyncClientFactory() = default;
      17             : 
      18             :   /**
      19             :    * Create a gRPC::RawAsyncClient.
      20             :    * Prefer AsyncClientManager::getOrCreateRawAsyncClient() to creating uncached raw async client
      21             :    * from factory directly. Only call this method when the raw async client must be owned
      22             :    * exclusively. For example, some filters pass *this reference to raw client. In this case, the
      23             :    * client must be destroyed before the filter instance. In this case, the grpc client must be
      24             :    * owned by the filter instance exclusively.
      25             :    * @return RawAsyncClientPtr async client.
      26             :    */
      27             :   virtual RawAsyncClientPtr createUncachedRawAsyncClient() PURE;
      28             : 
      29             : private:
      30             :   friend class AsyncClientFactoryImpl;
      31             : };
      32             : 
      33             : using AsyncClientFactoryPtr = std::unique_ptr<AsyncClientFactory>;
      34             : 
      35             : class GrpcServiceConfigWithHashKey {
      36             : public:
      37             :   GrpcServiceConfigWithHashKey() = default;
      38             : 
      39             :   explicit GrpcServiceConfigWithHashKey(const envoy::config::core::v3::GrpcService& config)
      40         149 :       : config_(config), pre_computed_hash_(Envoy::MessageUtil::hash(config)){};
      41             : 
      42          45 :   template <typename H> friend H AbslHashValue(H h, const GrpcServiceConfigWithHashKey& wrapper) {
      43          45 :     return H::combine(std::move(h), wrapper.pre_computed_hash_);
      44          45 :   }
      45             : 
      46           0 :   std::size_t getPreComputedHash() const { return pre_computed_hash_; }
      47             : 
      48             :   friend bool operator==(const GrpcServiceConfigWithHashKey& lhs,
      49          35 :                          const GrpcServiceConfigWithHashKey& rhs) {
      50          35 :     if (lhs.pre_computed_hash_ == rhs.pre_computed_hash_) {
      51          35 :       return Protobuf::util::MessageDifferencer::Equivalent(lhs.config_, rhs.config_);
      52          35 :     }
      53           0 :     return false;
      54          35 :   }
      55             : 
      56           5 :   const envoy::config::core::v3::GrpcService& config() const { return config_; }
      57             : 
      58           0 :   void setConfig(const envoy::config::core::v3::GrpcService g) {
      59           0 :     config_ = g;
      60           0 :     pre_computed_hash_ = Envoy::MessageUtil::hash(g);
      61           0 :   }
      62             : 
      63             : private:
      64             :   envoy::config::core::v3::GrpcService config_;
      65             :   std::size_t pre_computed_hash_;
      66             : };
      67             : 
      68             : // Singleton gRPC client manager. Grpc::AsyncClientManager can be used to create per-service
      69             : // Grpc::AsyncClientFactory instances. All manufactured Grpc::AsyncClients must
      70             : // be destroyed before the AsyncClientManager can be safely destructed.
      71             : class AsyncClientManager {
      72             : public:
      73         318 :   virtual ~AsyncClientManager() = default;
      74             : 
      75             :   // TODO(diazalan) deprecate old getOrCreateRawAsyncClient once all filters have been transitioned
      76             :   /**
      77             :    * Create a Grpc::RawAsyncClient. The async client is cached thread locally and shared across
      78             :    * different filter instances.
      79             :    * @param grpc_service envoy::config::core::v3::GrpcService configuration.
      80             :    * @param scope stats scope.
      81             :    * @param skip_cluster_check if set to true skips checks for cluster presence and being statically
      82             :    * configured.
      83             :    * @param cache_option always use cache or use cache when runtime is enabled.
      84             :    * @return RawAsyncClientPtr a grpc async client.
      85             :    * @throws EnvoyException when grpc_service validation fails.
      86             :    */
      87             :   virtual RawAsyncClientSharedPtr
      88             :   getOrCreateRawAsyncClient(const envoy::config::core::v3::GrpcService& grpc_service,
      89             :                             Stats::Scope& scope, bool skip_cluster_check) PURE;
      90             : 
      91             :   /**
      92             :    * Create a Grpc::RawAsyncClient. The async client is cached thread locally and shared across
      93             :    * different filter instances.
      94             :    * @param grpc_service Envoy::Grpc::GrpcServiceConfigWithHashKey which contains config and
      95             :    * hashkey.
      96             :    * @param scope stats scope.
      97             :    * @param skip_cluster_check if set to true skips checks for cluster presence and being statically
      98             :    * configured.
      99             :    * @param cache_option always use cache or use cache when runtime is enabled.
     100             :    * @return RawAsyncClientPtr a grpc async client.
     101             :    * @throws EnvoyException when grpc_service validation fails.
     102             :    */
     103             :   virtual RawAsyncClientSharedPtr
     104             :   getOrCreateRawAsyncClientWithHashKey(const GrpcServiceConfigWithHashKey& grpc_service,
     105             :                                        Stats::Scope& scope, bool skip_cluster_check) PURE;
     106             : 
     107             :   /**
     108             :    * Create a Grpc::AsyncClients factory for a service. Validation of the service is performed and
     109             :    * will raise an exception on failure.
     110             :    * @param grpc_service envoy::config::core::v3::GrpcService configuration.
     111             :    * @param scope stats scope.
     112             :    * @param skip_cluster_check if set to true skips checks for cluster presence and being statically
     113             :    * configured.
     114             :    * @return AsyncClientFactoryPtr factory for grpc_service.
     115             :    * @throws EnvoyException when grpc_service validation fails.
     116             :    */
     117             :   virtual AsyncClientFactoryPtr
     118             :   factoryForGrpcService(const envoy::config::core::v3::GrpcService& grpc_service,
     119             :                         Stats::Scope& scope, bool skip_cluster_check) PURE;
     120             : };
     121             : 
     122             : using AsyncClientManagerPtr = std::unique_ptr<AsyncClientManager>;
     123             : 
     124             : } // namespace Grpc
     125             : } // namespace Envoy

Generated by: LCOV version 1.15