Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/config/listener/v3/listener.pb.h" 4 : #include "envoy/network/connection_balancer.h" 5 : #include "envoy/registry/registry.h" 6 : #include "envoy/server/filter_config.h" 7 : 8 : #include "source/common/protobuf/protobuf.h" 9 : 10 : #include "absl/synchronization/mutex.h" 11 : 12 : namespace Envoy { 13 : namespace Network { 14 : /** 15 : * A base factory to create connection balance, which makes it easier to extend. 16 : */ 17 : class ConnectionBalanceFactory : public Config::TypedFactory { 18 : public: 19 : ~ConnectionBalanceFactory() override = default; 20 : virtual ConnectionBalancerSharedPtr 21 : createConnectionBalancerFromProto(const Protobuf::Message& config, 22 : Server::Configuration::FactoryContext& context) PURE; 23 : 24 0 : std::string category() const override { return "envoy.network.connection_balance"; } 25 : }; 26 : 27 : /** 28 : * Implementation of connection balancer that does exact balancing. This means that a lock is held 29 : * during balancing so that connection counts are nearly exactly balanced between handlers. This 30 : * is "nearly" exact in the sense that a connection might close in parallel thus making the counts 31 : * incorrect, but this should be rectified on the next accept. This balancer sacrifices accept 32 : * throughput for accuracy and should be used when there are a small number of connections that 33 : * rarely cycle (e.g., service mesh gRPC egress). 34 : */ 35 : class ExactConnectionBalancerImpl : public ConnectionBalancer { 36 : public: 37 : // ConnectionBalancer 38 : void registerHandler(BalancedConnectionHandler& handler) override; 39 : void unregisterHandler(BalancedConnectionHandler& handler) override; 40 : BalancedConnectionHandler& pickTargetHandler(BalancedConnectionHandler& current_handler) override; 41 : 42 : private: 43 : absl::Mutex lock_; 44 : std::vector<BalancedConnectionHandler*> handlers_ ABSL_GUARDED_BY(lock_); 45 : }; 46 : 47 : /** 48 : * A NOP connection balancer implementation that always continues execution after incrementing 49 : * the handler's connection count. 50 : */ 51 : class NopConnectionBalancerImpl : public ConnectionBalancer { 52 : public: 53 : // ConnectionBalancer 54 607 : void registerHandler(BalancedConnectionHandler&) override {} 55 607 : void unregisterHandler(BalancedConnectionHandler&) override {} 56 : BalancedConnectionHandler& 57 1315 : pickTargetHandler(BalancedConnectionHandler& current_handler) override { 58 : // In the NOP case just increment the connection count and return the current handler. 59 1315 : current_handler.incNumConnections(); 60 1315 : return current_handler; 61 1315 : } 62 : }; 63 : 64 : } // namespace Network 65 : } // namespace Envoy