Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/extensions/load_balancing_policies/least_request/v3/least_request.pb.h" 4 : #include "envoy/extensions/load_balancing_policies/least_request/v3/least_request.pb.validate.h" 5 : #include "envoy/upstream/load_balancer.h" 6 : 7 : #include "source/common/common/logger.h" 8 : #include "source/common/upstream/load_balancer_impl.h" 9 : #include "source/extensions/load_balancing_policies/common/factory_base.h" 10 : 11 : namespace Envoy { 12 : namespace Extensions { 13 : namespace LoadBalancingPolices { 14 : namespace LeastRequest { 15 : 16 : using LeastRequestLbProto = 17 : envoy::extensions::load_balancing_policies::least_request::v3::LeastRequest; 18 : using ClusterProto = envoy::config::cluster::v3::Cluster; 19 : using LegacyLeastRequestLbProto = ClusterProto::LeastRequestLbConfig; 20 : 21 : /** 22 : * Load balancer config that used to wrap the legacy least request config. 23 : */ 24 : class LegacyLeastRequestLbConfig : public Upstream::LoadBalancerConfig { 25 : public: 26 : LegacyLeastRequestLbConfig(const ClusterProto& cluster); 27 : 28 0 : OptRef<const LegacyLeastRequestLbProto> lbConfig() const { 29 0 : if (lb_config_.has_value()) { 30 0 : return lb_config_.value(); 31 0 : } 32 0 : return {}; 33 0 : }; 34 : 35 : private: 36 : absl::optional<LegacyLeastRequestLbProto> lb_config_; 37 : }; 38 : 39 : /** 40 : * Load balancer config that used to wrap the least request config. 41 : */ 42 : class TypedLeastRequestLbConfig : public Upstream::LoadBalancerConfig { 43 : public: 44 : TypedLeastRequestLbConfig(const LeastRequestLbProto& lb_config); 45 : 46 : const LeastRequestLbProto lb_config_; 47 : }; 48 : 49 : struct LeastRequestCreator : public Logger::Loggable<Logger::Id::upstream> { 50 : Upstream::LoadBalancerPtr operator()(Upstream::LoadBalancerParams params, 51 : OptRef<const Upstream::LoadBalancerConfig> lb_config, 52 : const Upstream::ClusterInfo& cluster_info, 53 : const Upstream::PrioritySet& priority_set, 54 : Runtime::Loader& runtime, Random::RandomGenerator& random, 55 : TimeSource& time_source); 56 : }; 57 : 58 : class Factory : public Common::FactoryBase<LeastRequestLbProto, LeastRequestCreator> { 59 : public: 60 13 : Factory() : FactoryBase("envoy.load_balancing_policies.least_request") {} 61 : 62 : Upstream::LoadBalancerConfigPtr loadConfig(const Protobuf::Message& config, 63 0 : ProtobufMessage::ValidationVisitor&) override { 64 : 65 0 : auto active_or_legacy = Common::ActiveOrLegacy<LeastRequestLbProto, ClusterProto>::get(&config); 66 0 : ASSERT(active_or_legacy.hasLegacy() || active_or_legacy.hasActive()); 67 : 68 0 : return active_or_legacy.hasLegacy() 69 0 : ? Upstream::LoadBalancerConfigPtr{new LegacyLeastRequestLbConfig( 70 0 : *active_or_legacy.legacy())} 71 0 : : Upstream::LoadBalancerConfigPtr{ 72 0 : new TypedLeastRequestLbConfig(*active_or_legacy.active())}; 73 0 : } 74 : }; 75 : 76 : DECLARE_FACTORY(Factory); 77 : 78 : } // namespace LeastRequest 79 : } // namespace LoadBalancingPolices 80 : } // namespace Extensions 81 : } // namespace Envoy