Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/server/factory_context.h" 4 : #include "envoy/upstream/upstream.h" 5 : 6 : #include "absl/container/flat_hash_map.h" 7 : 8 : namespace Envoy { 9 : namespace Extensions { 10 : namespace Common { 11 : namespace DynamicForwardProxy { 12 : 13 : class DfpCluster { 14 : public: 15 0 : virtual ~DfpCluster() = default; 16 : 17 : /** 18 : * @return the cluster enabled subCluster configuration or not. 19 : */ 20 : virtual bool enableSubCluster() const PURE; 21 : 22 : /** 23 : * Create a full cluster config for the cluster_name, with the specified host and port. 24 : * @return true and nullptr when the subCluster with the specified cluster_name already 25 : * created, or true and the created cluster config when it not exists and not 26 : * reach the limitation of max_sub_clusters, otherwise, return false and nullptr. 27 : */ 28 : virtual std::pair<bool, absl::optional<envoy::config::cluster::v3::Cluster>> 29 : createSubClusterConfig(const std::string& cluster_name, const std::string& host, 30 : const int port) PURE; 31 : 32 : /** 33 : * Update the last used time of the subCluster with the specified cluster_name. 34 : * @return true if the subCluster is existing. 35 : */ 36 : virtual bool touch(const std::string& cluster_name) PURE; 37 : }; 38 : 39 : using DfpClusterSharedPtr = std::shared_ptr<DfpCluster>; 40 : using DfpClusterWeakPtr = std::weak_ptr<DfpCluster>; 41 : 42 : class DFPClusterStore : public Singleton::Instance { 43 : public: 44 : // Load the dynamic forward proxy cluster from this store. 45 : DfpClusterSharedPtr load(std::string cluster_name); 46 : 47 : // Save the dynamic forward proxy cluster into this store. 48 : void save(const std::string cluster_name, DfpClusterSharedPtr cluster); 49 : 50 : // Remove the dynamic forward proxy cluster from this store. 51 : void remove(std::string cluster_name); 52 : 53 : private: 54 : using ClusterMapType = absl::flat_hash_map<std::string, DfpClusterWeakPtr>; 55 : struct ClusterStoreType { 56 : ClusterMapType map_ ABSL_GUARDED_BY(mutex_); 57 : absl::Mutex mutex_; 58 : }; 59 : 60 0 : ClusterStoreType& getClusterStore() { MUTABLE_CONSTRUCT_ON_FIRST_USE(ClusterStoreType); } 61 : }; 62 : 63 : using DFPClusterStoreSharedPtr = std::shared_ptr<DFPClusterStore>; 64 : 65 : class DFPClusterStoreFactory { 66 : public: 67 : DFPClusterStoreFactory(Singleton::Manager& singleton_manager) 68 5 : : singleton_manager_(singleton_manager) {} 69 : DFPClusterStoreSharedPtr get(); 70 : 71 : private: 72 : Singleton::Manager& singleton_manager_; 73 : }; 74 : 75 : } // namespace DynamicForwardProxy 76 : } // namespace Common 77 : } // namespace Extensions 78 : } // namespace Envoy