LCOV - code coverage report
Current view: top level - source/extensions/load_balancing_policies/common - factory_base.h (source / functions) Hit Total Coverage
Test: coverage.dat Lines: 29 38 76.3 %
Date: 2024-01-05 06:35:25 Functions: 18 96 18.8 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <memory>
       4             : 
       5             : #include "envoy/upstream/load_balancer.h"
       6             : 
       7             : #include "source/common/upstream/load_balancer_factory_base.h"
       8             : 
       9             : namespace Envoy {
      10             : namespace Extensions {
      11             : namespace LoadBalancingPolices {
      12             : namespace Common {
      13             : 
      14             : template <class ProtoType, class Impl>
      15             : class FactoryBase : public Upstream::TypedLoadBalancerFactoryBase<ProtoType> {
      16             : public:
      17          39 :   FactoryBase(const std::string& name) : Upstream::TypedLoadBalancerFactoryBase<ProtoType>(name) {}
      18             : 
      19             :   Upstream::ThreadAwareLoadBalancerPtr create(OptRef<const Upstream::LoadBalancerConfig> lb_config,
      20             :                                               const Upstream::ClusterInfo& cluster_info,
      21             :                                               const Upstream::PrioritySet& priority_set,
      22             :                                               Runtime::Loader& runtime,
      23             :                                               Envoy::Random::RandomGenerator& random,
      24         159 :                                               TimeSource& time_source) override {
      25             : 
      26         159 :     return std::make_unique<ThreadAwareLb>(std::make_shared<LbFactory>(
      27         159 :         lb_config, cluster_info, priority_set, runtime, random, time_source));
      28         159 :   }
      29             : 
      30             : private:
      31             :   class LbFactory : public Upstream::LoadBalancerFactory {
      32             :   public:
      33             :     LbFactory(OptRef<const Upstream::LoadBalancerConfig> lb_config,
      34             :               const Upstream::ClusterInfo& cluster_info, const Upstream::PrioritySet& priority_set,
      35             :               Runtime::Loader& runtime, Envoy::Random::RandomGenerator& random,
      36             :               TimeSource& time_source)
      37             :         : lb_config_(lb_config), cluster_info_(cluster_info), priority_set_(priority_set),
      38         159 :           runtime_(runtime), random_(random), time_source_(time_source) {}
      39             : 
      40         306 :     Upstream::LoadBalancerPtr create(Upstream::LoadBalancerParams params) override {
      41         306 :       return Impl()(params, lb_config_, cluster_info_, priority_set_, runtime_, random_,
      42         306 :                     time_source_);
      43         306 :     }
      44             : 
      45         306 :     bool recreateOnHostChange() const override { return false; }
      46             : 
      47             :   public:
      48             :     OptRef<const Upstream::LoadBalancerConfig> lb_config_;
      49             : 
      50             :     const Upstream::ClusterInfo& cluster_info_;
      51             :     const Upstream::PrioritySet& priority_set_;
      52             :     Runtime::Loader& runtime_;
      53             :     Envoy::Random::RandomGenerator& random_;
      54             :     TimeSource& time_source_;
      55             :   };
      56             : 
      57             :   class ThreadAwareLb : public Upstream::ThreadAwareLoadBalancer {
      58             :   public:
      59         159 :     ThreadAwareLb(Upstream::LoadBalancerFactorySharedPtr factory) : factory_(std::move(factory)) {}
      60             : 
      61         159 :     Upstream::LoadBalancerFactorySharedPtr factory() override { return factory_; }
      62         159 :     void initialize() override {}
      63             : 
      64             :   private:
      65             :     Upstream::LoadBalancerFactorySharedPtr factory_;
      66             :   };
      67             : 
      68             :   const std::string name_;
      69             : };
      70             : 
      71             : /**
      72             :  * Helper class to hold either a legacy or production config.
      73             :  */
      74             : template <class ActiveType, class LegacyType> class ActiveOrLegacy {
      75             : public:
      76         465 :   template <class BaseType> static ActiveOrLegacy get(const BaseType* base) {
      77         465 :     auto* active_type = dynamic_cast<const ActiveType*>(base);
      78         465 :     if (active_type != nullptr) {
      79           0 :       return {active_type};
      80           0 :     }
      81         465 :     auto* legacy_type = dynamic_cast<const LegacyType*>(base);
      82         465 :     if (legacy_type != nullptr) {
      83         465 :       return {legacy_type};
      84         465 :     }
      85             : 
      86           0 :     return {};
      87         465 :   }
      88             : 
      89         306 :   bool hasActive() const { return active_ != nullptr; }
      90         159 :   bool hasLegacy() const { return legacy_ != nullptr; }
      91             : 
      92           0 :   const ActiveType* active() const {
      93           0 :     ASSERT(hasActive());
      94           0 :     return active_;
      95           0 :   }
      96         465 :   const LegacyType* legacy() const {
      97         465 :     ASSERT(hasLegacy());
      98         465 :     return legacy_;
      99         465 :   }
     100             : 
     101             : private:
     102           0 :   ActiveOrLegacy() = default;
     103           0 :   ActiveOrLegacy(const ActiveType* active) : active_(active) {}
     104         465 :   ActiveOrLegacy(const LegacyType* legacy) : legacy_(legacy) {}
     105             : 
     106             :   const ActiveType* active_{};
     107             :   const LegacyType* legacy_{};
     108             : };
     109             : 
     110             : } // namespace Common
     111             : } // namespace LoadBalancingPolices
     112             : } // namespace Extensions
     113             : } // namespace Envoy

Generated by: LCOV version 1.15