Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/common/pure.h" 4 : #include "envoy/http/async_client.h" 5 : #include "envoy/tcp/async_tcp_client.h" 6 : #include "envoy/upstream/load_balancer.h" 7 : #include "envoy/upstream/upstream.h" 8 : 9 : namespace Envoy { 10 : namespace Upstream { 11 : 12 : // HttpPoolData returns information about a given pool as well as a function 13 : // to create streams on that pool. 14 : class HttpPoolData { 15 : public: 16 : using OnNewStreamFn = std::function<void()>; 17 : 18 : HttpPoolData(OnNewStreamFn on_new_stream, Http::ConnectionPool::Instance* pool) 19 459 : : on_new_stream_(on_new_stream), pool_(pool) {} 20 : 21 : /** 22 : * See documentation of Http::ConnectionPool::Instance. 23 : */ 24 : Envoy::Http::ConnectionPool::Cancellable* 25 : newStream(Http::ResponseDecoder& response_decoder, 26 : Envoy::Http::ConnectionPool::Callbacks& callbacks, 27 251 : const Http::ConnectionPool::Instance::StreamOptions& stream_options) { 28 251 : on_new_stream_(); 29 251 : return pool_->newStream(response_decoder, callbacks, stream_options); 30 251 : } 31 0 : bool hasActiveConnections() const { return pool_->hasActiveConnections(); }; 32 : 33 : /** 34 : * See documentation of Envoy::ConnectionPool::Instance. 35 : */ 36 0 : void addIdleCallback(ConnectionPool::Instance::IdleCb cb) { pool_->addIdleCallback(cb); }; 37 0 : void drainConnections(ConnectionPool::DrainBehavior drain_behavior) { 38 0 : pool_->drainConnections(drain_behavior); 39 0 : }; 40 : 41 502 : Upstream::HostDescriptionConstSharedPtr host() const { return pool_->host(); } 42 : 43 : private: 44 : friend class HttpPoolDataPeer; 45 : 46 : OnNewStreamFn on_new_stream_; 47 : Http::ConnectionPool::Instance* pool_; 48 : }; 49 : 50 : // Tcp pool returns information about a given pool, as well as a function to 51 : // create connections on that pool. 52 : class TcpPoolData { 53 : public: 54 : using OnNewConnectionFn = std::function<void()>; 55 : 56 : TcpPoolData(OnNewConnectionFn on_new_connection, Tcp::ConnectionPool::Instance* pool) 57 208 : : on_new_connection_(on_new_connection), pool_(pool) {} 58 : 59 : Envoy::Tcp::ConnectionPool::Cancellable* 60 0 : newConnection(Envoy::Tcp::ConnectionPool::Callbacks& callbacks) { 61 0 : on_new_connection_(); 62 0 : return pool_->newConnection(callbacks); 63 0 : } 64 : 65 0 : Upstream::HostDescriptionConstSharedPtr host() const { return pool_->host(); } 66 : 67 : private: 68 : friend class TcpPoolDataPeer; 69 : OnNewConnectionFn on_new_connection_; 70 : Tcp::ConnectionPool::Instance* pool_; 71 : }; 72 : 73 : /** 74 : * A thread local cluster instance that can be used for direct load balancing and host set 75 : * interactions. In general, an instance of ThreadLocalCluster can only be safely used in the 76 : * direct call context after it is retrieved from the cluster manager. See ClusterManager::get() 77 : * for more information. 78 : */ 79 : class ThreadLocalCluster { 80 : public: 81 514 : virtual ~ThreadLocalCluster() = default; 82 : 83 : /** 84 : * @return const PrioritySet& the backing priority set. 85 : */ 86 : virtual const PrioritySet& prioritySet() PURE; 87 : 88 : /** 89 : * @return ClusterInfoConstSharedPtr the info for this cluster. The info is safe to store beyond 90 : * the lifetime of the ThreadLocalCluster instance itself. 91 : */ 92 : virtual ClusterInfoConstSharedPtr info() PURE; 93 : 94 : /** 95 : * @return LoadBalancer& the backing load balancer. 96 : */ 97 : virtual LoadBalancer& loadBalancer() PURE; 98 : 99 : /** 100 : * Allocate a load balanced HTTP connection pool for a cluster. This is *per-thread* so that 101 : * callers do not need to worry about per thread synchronization. The load balancing policy that 102 : * is used is the one defined on the cluster when it was created. 103 : * 104 : * @param priority the connection pool priority. 105 : * @param downstream_protocol the downstream protocol (if one exists) to use in protocol 106 : * selection. 107 : * @param context the optional load balancer context. Must continue to be 108 : * valid until newConnection is called on the pool (if it is to be called). 109 : * @return the connection pool data or nullopt if there is no host available in the cluster. 110 : */ 111 : virtual absl::optional<HttpPoolData> 112 : httpConnPool(ResourcePriority priority, absl::optional<Http::Protocol> downstream_protocol, 113 : LoadBalancerContext* context) PURE; 114 : 115 : /** 116 : * Allocate a load balanced TCP connection pool for a cluster. This is *per-thread* so that 117 : * callers do not need to worry about per thread synchronization. The load balancing policy that 118 : * is used is the one defined on the cluster when it was created. 119 : * 120 : * @param priority the connection pool priority. 121 : * @param context the optional load balancer context. Must continue to be 122 : * valid until newConnection is called on the pool (if it is to be called). 123 : * @return the connection pool data or nullopt if there is no host available in the cluster. 124 : */ 125 : virtual absl::optional<TcpPoolData> tcpConnPool(ResourcePriority priority, 126 : LoadBalancerContext* context) PURE; 127 : 128 : /** 129 : * Allocate a load balanced TCP connection for a cluster. The created connection is already 130 : * bound to the correct *per-thread* dispatcher, so no further synchronization is needed. The 131 : * load balancing policy that is used is the one defined on the cluster when it was created. 132 : * 133 : * @param context the optional load balancer context. 134 : * @return both a connection and the host that backs the connection. Both can be nullptr if there 135 : * is no host available in the cluster. 136 : */ 137 : virtual Host::CreateConnectionData tcpConn(LoadBalancerContext* context) PURE; 138 : 139 : /** 140 : * @return a client that can be used to make async HTTP calls against the given cluster. The 141 : * client may be backed by a connection pool or by a multiplexed connection. The cluster manager 142 : * owns the client. 143 : */ 144 : virtual Http::AsyncClient& httpAsyncClient() PURE; 145 : 146 : /** 147 : * @param context the optional load balancer context. 148 : * @param options the tcp client creation config options. 149 : * @return a client that can be used to make async Tcp calls against the given cluster. 150 : */ 151 : virtual Tcp::AsyncTcpClientPtr 152 : tcpAsyncClient(LoadBalancerContext* context, 153 : Tcp::AsyncTcpClientOptionsConstSharedPtr options) PURE; 154 : 155 : /** 156 : * @return the thread local cluster drop_overload configuration. 157 : */ 158 : virtual UnitFloat dropOverload() const PURE; 159 : 160 : /** 161 : * Set up the drop_overload value for the thread local cluster. 162 : */ 163 : virtual void setDropOverload(UnitFloat drop_overload) PURE; 164 : }; 165 : 166 : using ThreadLocalClusterOptRef = absl::optional<std::reference_wrapper<ThreadLocalCluster>>; 167 : 168 : } // namespace Upstream 169 : } // namespace Envoy