Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/network/listen_socket.h" 4 : 5 : namespace Envoy { 6 : namespace Network { 7 : 8 : /** 9 : * A connection handler that is balanced. Typically implemented by individual listeners depending 10 : * on their balancing configuration. 11 : */ 12 : class BalancedConnectionHandler { 13 : public: 14 607 : virtual ~BalancedConnectionHandler() = default; 15 : 16 : /** 17 : * @return the number of active connections within the handler. 18 : */ 19 : virtual uint64_t numConnections() const PURE; 20 : 21 : /** 22 : * Increment the number of connections within the handler. This must be called by a connection 23 : * balancer implementation prior to a connection being picked via pickTargetHandler(). This makes 24 : * sure that connection counts are accurate during connection transfer (i.e., that the target 25 : * balancer accounts for the incoming connection). This is done by the balancer vs. the 26 : * connection handler to account for different locking needs inside the balancer. 27 : */ 28 : virtual void incNumConnections() PURE; 29 : 30 : /** 31 : * Post a connected socket to this connection handler. This is used for cross-thread connection 32 : * transfer during the balancing process. 33 : */ 34 : virtual void post(Network::ConnectionSocketPtr&& socket) PURE; 35 : 36 : virtual void onAcceptWorker(Network::ConnectionSocketPtr&& socket, 37 : bool hand_off_restored_destination_connections, bool rebalanced) PURE; 38 : }; 39 : 40 : /** 41 : * An implementation of a connection balancer. This abstracts the underlying policy (e.g., exact, 42 : * fuzzy, etc.). 43 : */ 44 : class ConnectionBalancer { 45 : public: 46 622 : virtual ~ConnectionBalancer() = default; 47 : 48 : /** 49 : * Register a new handler with the balancer that is available for balancing. 50 : */ 51 : virtual void registerHandler(BalancedConnectionHandler& handler) PURE; 52 : 53 : /** 54 : * Unregister a handler with the balancer that is no longer available for balancing. 55 : */ 56 : virtual void unregisterHandler(BalancedConnectionHandler& handler) PURE; 57 : 58 : /** 59 : * Pick a target handler to send a connection to. 60 : * @param current_handler supplies the currently executing connection handler. 61 : * @return current_handler if the connection should stay bound to the current handler, or a 62 : * different handler if the connection should be rebalanced. 63 : * 64 : * NOTE: It is the responsibility of the balancer to call incNumConnections() on the returned 65 : * balancer. See the comments above for more explanation. 66 : */ 67 : virtual BalancedConnectionHandler& 68 : pickTargetHandler(BalancedConnectionHandler& current_handler) PURE; 69 : }; 70 : 71 : using ConnectionBalancerSharedPtr = std::shared_ptr<ConnectionBalancer>; 72 : using BalancedConnectionHandlerOptRef = 73 : absl::optional<std::reference_wrapper<BalancedConnectionHandler>>; 74 : 75 : } // namespace Network 76 : } // namespace Envoy