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 DfpLb {
14
public:
15
392
  virtual ~DfpLb() = default;
16
  virtual Upstream::HostConstSharedPtr findHostByName(const std::string& host) const PURE;
17
};
18

            
19
class DfpCluster {
20
public:
21
162
  virtual ~DfpCluster() = default;
22

            
23
  /**
24
   * @return the cluster enabled subCluster configuration or not.
25
   */
26
  virtual bool enableSubCluster() const PURE;
27

            
28
  /**
29
   * Create a full cluster config for the cluster_name, with the specified host and port.
30
   * @return true and nullptr when the subCluster with the specified cluster_name already
31
   *         created, or true and the created cluster config when it not exists and not
32
   *         reach the limitation of max_sub_clusters, otherwise, return false and nullptr.
33
   */
34
  virtual std::pair<bool, absl::optional<envoy::config::cluster::v3::Cluster>>
35
  createSubClusterConfig(const std::string& cluster_name, const std::string& host,
36
                         const int port) PURE;
37

            
38
  /**
39
   * Update the last used time of the subCluster with the specified cluster_name.
40
   * @return true if the subCluster is existing.
41
   */
42
  virtual bool touch(const std::string& cluster_name) PURE;
43
};
44

            
45
using DfpClusterSharedPtr = std::shared_ptr<DfpCluster>;
46
using DfpClusterWeakPtr = std::weak_ptr<DfpCluster>;
47

            
48
class DFPClusterStore : public Singleton::Instance {
49
public:
50
  // Load the dynamic forward proxy cluster from this store.
51
  DfpClusterSharedPtr load(std::string cluster_name);
52

            
53
  // Save the dynamic forward proxy cluster into this store.
54
  void save(const std::string cluster_name, DfpClusterSharedPtr cluster);
55

            
56
  // Remove the dynamic forward proxy cluster from this store.
57
  void remove(std::string cluster_name);
58

            
59
private:
60
  using ClusterMapType = absl::flat_hash_map<std::string, DfpClusterWeakPtr>;
61
  struct ClusterStoreType {
62
    ClusterMapType map_ ABSL_GUARDED_BY(mutex_);
63
    absl::Mutex mutex_;
64
  };
65

            
66
203
  ClusterStoreType& getClusterStore() { MUTABLE_CONSTRUCT_ON_FIRST_USE(ClusterStoreType); }
67
};
68

            
69
using DFPClusterStoreSharedPtr = std::shared_ptr<DFPClusterStore>;
70

            
71
class DFPClusterStoreFactory {
72
public:
73
  DFPClusterStoreFactory(Singleton::Manager& singleton_manager)
74
199
      : singleton_manager_(singleton_manager) {}
75
  DFPClusterStoreSharedPtr get();
76

            
77
private:
78
  Singleton::Manager& singleton_manager_;
79
};
80

            
81
} // namespace DynamicForwardProxy
82
} // namespace Common
83
} // namespace Extensions
84
} // namespace Envoy