Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/event/dispatcher.h" 4 : #include "envoy/upstream/resource_manager.h" 5 : #include "envoy/upstream/upstream.h" 6 : 7 : #include "source/common/upstream/conn_pool_map.h" 8 : 9 : namespace Envoy { 10 : namespace Upstream { 11 : /** 12 : * A class mapping keys to connection pools, with some recycling logic built in. 13 : */ 14 : template <typename KEY_TYPE, typename POOL_TYPE> class PriorityConnPoolMap { 15 : public: 16 : using ConnPoolMapType = ConnPoolMap<KEY_TYPE, POOL_TYPE>; 17 : using PoolFactory = typename ConnPoolMapType::PoolFactory; 18 : using IdleCb = typename ConnPoolMapType::IdleCb; 19 : using PoolOptRef = typename ConnPoolMapType::PoolOptRef; 20 : 21 : PriorityConnPoolMap(Event::Dispatcher& dispatcher, const HostConstSharedPtr& host); 22 173 : ~PriorityConnPoolMap(); 23 : /** 24 : * Returns an existing pool for the given priority and `key`, or creates a new one using 25 : * `factory`. Note that it is possible for this to fail if a limit on the number of pools allowed 26 : * is reached. 27 : * @return The pool corresponding to `key`, or `absl::nullopt`. 28 : */ 29 : PoolOptRef getPool(ResourcePriority priority, const KEY_TYPE& key, const PoolFactory& factory); 30 : 31 : /** 32 : * Erase a pool for the given priority and `key` if it exists and is idle. 33 : */ 34 : bool erasePool(ResourcePriority priority, const KEY_TYPE& key); 35 : 36 : /** 37 : * @return the number of pools across all priorities. 38 : */ 39 : size_t size() const; 40 : 41 : /** 42 : * @return true if the pools across all priorities are empty. 43 : */ 44 : bool empty() const; 45 : 46 : /** 47 : * Destroys all mapped pools. 48 : */ 49 : void clear(); 50 : 51 : /** 52 : * Adds a drain callback to all mapped pools. Any future mapped pools with have the callback 53 : * automatically added. Be careful with the callback. If it itself calls into `this`, modifying 54 : * the state of `this`, there is a good chance it will cause corruption due to the callback firing 55 : * immediately. 56 : */ 57 : void addIdleCallback(const IdleCb& cb); 58 : 59 : /** 60 : * See `Envoy::ConnectionPool::Instance::drainConnections()`. 61 : */ 62 : void drainConnections(Envoy::ConnectionPool::DrainBehavior drain_behavior); 63 : 64 : private: 65 : size_t getPriorityIndex(ResourcePriority priority) const; 66 : 67 : std::array<std::unique_ptr<ConnPoolMapType>, NumResourcePriorities> conn_pool_maps_; 68 : }; 69 : 70 : } // namespace Upstream 71 : } // namespace Envoy