/proc/self/cwd/source/extensions/filters/network/local_ratelimit/local_ratelimit.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include "envoy/event/timer.h" |
4 | | #include "envoy/extensions/filters/network/local_ratelimit/v3/local_rate_limit.pb.h" |
5 | | #include "envoy/network/filter.h" |
6 | | #include "envoy/runtime/runtime.h" |
7 | | #include "envoy/singleton/manager.h" |
8 | | #include "envoy/stats/stats_macros.h" |
9 | | |
10 | | #include "source/common/common/thread_synchronizer.h" |
11 | | #include "source/common/runtime/runtime_protos.h" |
12 | | #include "source/extensions/filters/common/local_ratelimit/local_ratelimit_impl.h" |
13 | | |
14 | | namespace Envoy { |
15 | | namespace Extensions { |
16 | | namespace NetworkFilters { |
17 | | namespace LocalRateLimitFilter { |
18 | | |
19 | | /** |
20 | | * All local rate limit stats. @see stats_macros.h |
21 | | */ |
22 | 1.16k | #define ALL_LOCAL_RATE_LIMIT_STATS(COUNTER) COUNTER(rate_limited) |
23 | | |
24 | | /** |
25 | | * Struct definition for all local rate limit stats. @see stats_macros.h |
26 | | */ |
27 | | struct LocalRateLimitStats { |
28 | | ALL_LOCAL_RATE_LIMIT_STATS(GENERATE_COUNTER_STRUCT) |
29 | | }; |
30 | | |
31 | | using LocalRateLimiterImplSharedPtr = |
32 | | std::shared_ptr<Filters::Common::LocalRateLimit::LocalRateLimiterImpl>; |
33 | | |
34 | | class SharedRateLimitSingleton : public Singleton::Instance, Logger::Loggable<Logger::Id::filter> { |
35 | | public: |
36 | | ~SharedRateLimitSingleton() override; |
37 | | |
38 | | class Key : public std::pair<std::string, envoy::type::v3::TokenBucket> { |
39 | | public: |
40 | | using std::pair<std::string, envoy::type::v3::TokenBucket>::pair; |
41 | | |
42 | | // Add absl::Hash support. |
43 | | template <typename H> |
44 | 534 | friend H AbslHashValue(H h, const Key& key) { // NOLINT(readability-identifier-naming) |
45 | 534 | return H::combine(std::move(h), key.first, MessageUtil::hash(key.second)); |
46 | 534 | } |
47 | | |
48 | 270 | bool operator==(const Key& that) const { |
49 | 270 | return (this->first == that.first) && |
50 | 270 | Protobuf::util::MessageDifferencer::Equivalent(this->second, that.second); |
51 | 270 | } |
52 | | }; |
53 | | |
54 | | std::pair<LocalRateLimiterImplSharedPtr, const Key*> |
55 | | get(const envoy::extensions::filters::network::local_ratelimit::v3::LocalRateLimit& proto_config, |
56 | | std::function<LocalRateLimiterImplSharedPtr()> create_fn); |
57 | | |
58 | | void removeIfUnused(const Key* key); |
59 | | |
60 | | private: |
61 | | // Use node_hash_map so that a stable pointer to Key can be returned. |
62 | | absl::node_hash_map<Key, std::weak_ptr<Filters::Common::LocalRateLimit::LocalRateLimiterImpl>> |
63 | | limiters_; |
64 | | }; |
65 | | |
66 | | /** |
67 | | * Configuration shared across all connections. Must be thread safe. |
68 | | */ |
69 | | class Config : Logger::Loggable<Logger::Id::filter> { |
70 | | public: |
71 | | Config( |
72 | | const envoy::extensions::filters::network::local_ratelimit::v3::LocalRateLimit& proto_config, |
73 | | Event::Dispatcher& dispatcher, Stats::Scope& scope, Runtime::Loader& runtime, |
74 | | Singleton::Manager& singleton_manager); |
75 | | |
76 | | ~Config(); |
77 | | |
78 | | bool canCreateConnection(); |
79 | 2.55k | bool enabled() { return enabled_.enabled(); } |
80 | 506 | LocalRateLimitStats& stats() { return stats_; } |
81 | | |
82 | | private: |
83 | | static LocalRateLimitStats generateStats(const std::string& prefix, Stats::Scope& scope); |
84 | | |
85 | | LocalRateLimiterImplSharedPtr rate_limiter_; |
86 | | Runtime::FeatureFlag enabled_; |
87 | | LocalRateLimitStats stats_; |
88 | | const SharedRateLimitSingleton::Key* shared_bucket_key_{}; |
89 | | std::shared_ptr<SharedRateLimitSingleton> shared_bucket_registry_; |
90 | | |
91 | | friend class LocalRateLimitTestBase; |
92 | | }; |
93 | | |
94 | | using ConfigSharedPtr = std::shared_ptr<Config>; |
95 | | |
96 | | /** |
97 | | * Per-connection local rate limit filter. |
98 | | */ |
99 | | class Filter : public Network::ReadFilter, Logger::Loggable<Logger::Id::filter> { |
100 | | public: |
101 | 1.16k | Filter(const ConfigSharedPtr& config) : config_(config) {} |
102 | | |
103 | | // Network::ReadFilter |
104 | 1.53k | Network::FilterStatus onData(Buffer::Instance&, bool) override { |
105 | 1.53k | return Network::FilterStatus::Continue; |
106 | 1.53k | } |
107 | | Network::FilterStatus onNewConnection() override; |
108 | 1.16k | void initializeReadFilterCallbacks(Network::ReadFilterCallbacks& read_callbacks) override { |
109 | 1.16k | read_callbacks_ = &read_callbacks; |
110 | 1.16k | } |
111 | | |
112 | | private: |
113 | | const ConfigSharedPtr config_; |
114 | | Network::ReadFilterCallbacks* read_callbacks_{}; |
115 | | }; |
116 | | |
117 | | } // namespace LocalRateLimitFilter |
118 | | } // namespace NetworkFilters |
119 | | } // namespace Extensions |
120 | | } // namespace Envoy |