Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/extensions/load_balancing_policies/random/v3/random.pb.h" 4 : #include "envoy/extensions/load_balancing_policies/random/v3/random.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 Random { 15 : 16 : using RandomLbProto = envoy::extensions::load_balancing_policies::random::v3::Random; 17 : 18 : /** 19 : * Empty load balancer config that used to represent the config for the random load balancer. 20 : */ 21 : class EmptyRandomLbConfig : public Upstream::LoadBalancerConfig { 22 : public: 23 0 : EmptyRandomLbConfig() = default; 24 : }; 25 : 26 : /** 27 : * Load balancer config that used to wrap the random config. 28 : */ 29 : class TypedRandomLbConfig : public Upstream::LoadBalancerConfig { 30 : public: 31 : TypedRandomLbConfig(const RandomLbProto& lb_config); 32 : 33 : const RandomLbProto lb_config_; 34 : }; 35 : 36 : struct RandomCreator : public Logger::Loggable<Logger::Id::upstream> { 37 : Upstream::LoadBalancerPtr operator()( 38 : Upstream::LoadBalancerParams params, OptRef<const Upstream::LoadBalancerConfig> lb_config, 39 : const Upstream::ClusterInfo& cluster_info, const Upstream::PrioritySet& priority_set, 40 : Runtime::Loader& runtime, Envoy::Random::RandomGenerator& random, TimeSource& time_source); 41 : }; 42 : 43 : class Factory : public Common::FactoryBase<RandomLbProto, RandomCreator> { 44 : public: 45 13 : Factory() : FactoryBase("envoy.load_balancing_policies.random") {} 46 : 47 : Upstream::LoadBalancerConfigPtr loadConfig(const Protobuf::Message& config, 48 0 : ProtobufMessage::ValidationVisitor&) override { 49 0 : auto typed_config = dynamic_cast<const RandomLbProto*>(&config); 50 0 : if (typed_config == nullptr) { 51 0 : return std::make_unique<EmptyRandomLbConfig>(); 52 0 : } 53 0 : return std::make_unique<TypedRandomLbConfig>(*typed_config); 54 0 : } 55 : }; 56 : 57 : DECLARE_FACTORY(Factory); 58 : 59 : } // namespace Random 60 : } // namespace LoadBalancingPolices 61 : } // namespace Extensions 62 : } // namespace Envoy