/proc/self/cwd/source/extensions/filters/network/connection_limit/connection_limit.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <chrono> |
4 | | #include <thread> |
5 | | |
6 | | #include "envoy/event/dispatcher.h" |
7 | | #include "envoy/event/timer.h" |
8 | | #include "envoy/extensions/filters/network/connection_limit/v3/connection_limit.pb.h" |
9 | | #include "envoy/network/filter.h" |
10 | | #include "envoy/runtime/runtime.h" |
11 | | #include "envoy/stats/stats_macros.h" |
12 | | |
13 | | #include "source/common/common/thread_synchronizer.h" |
14 | | #include "source/common/runtime/runtime_protos.h" |
15 | | |
16 | | namespace Envoy { |
17 | | namespace Extensions { |
18 | | namespace NetworkFilters { |
19 | | namespace ConnectionLimitFilter { |
20 | | |
21 | | /** |
22 | | * All connection limit stats. @see stats_macros.h |
23 | | */ |
24 | | #define ALL_CONNECTION_LIMIT_STATS(COUNTER, GAUGE) \ |
25 | 0 | COUNTER(limited_connections) \ |
26 | 0 | GAUGE(active_connections, Accumulate) |
27 | | |
28 | | /** |
29 | | * Struct definition for connection limit stats. @see stats_macros.h |
30 | | */ |
31 | | struct ConnectionLimitStats { |
32 | | ALL_CONNECTION_LIMIT_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT) |
33 | | }; |
34 | | |
35 | | /** |
36 | | * Configuration shared across all connections on a filter chain basis. |
37 | | */ |
38 | | class Config : Logger::Loggable<Logger::Id::filter> { |
39 | | public: |
40 | | Config(const envoy::extensions::filters::network::connection_limit::v3::ConnectionLimit& |
41 | | proto_config, |
42 | | Stats::Scope& scope, Runtime::Loader& runtime); |
43 | | |
44 | | bool incrementConnectionWithinLimit(); |
45 | | void incrementConnection(); |
46 | | void decrementConnection(); |
47 | 0 | bool enabled() { return enabled_.enabled(); } |
48 | 0 | absl::optional<std::chrono::milliseconds> delay() { return delay_; } |
49 | 0 | ConnectionLimitStats& stats() { return stats_; } |
50 | | |
51 | | private: |
52 | | static ConnectionLimitStats generateStats(const std::string& prefix, Stats::Scope& scope); |
53 | | Runtime::FeatureFlag enabled_; |
54 | | ConnectionLimitStats stats_; |
55 | | const uint64_t max_connections_; |
56 | | std::atomic<uint64_t> connections_; |
57 | | absl::optional<std::chrono::milliseconds> delay_; |
58 | | mutable Thread::ThreadSynchronizer synchronizer_; // Used for testing only. |
59 | | |
60 | | friend class ConnectionLimitTestBase; |
61 | | }; |
62 | | |
63 | | using ConfigSharedPtr = std::shared_ptr<Config>; |
64 | | |
65 | | /** |
66 | | * Per-connection connection limit filter |
67 | | */ |
68 | | class Filter : public Network::ReadFilter, |
69 | | public Network::ConnectionCallbacks, |
70 | | Logger::Loggable<Logger::Id::filter> { |
71 | | public: |
72 | 0 | Filter(const ConfigSharedPtr& config) : config_(config) {} |
73 | | |
74 | | // Network::ReadFilter |
75 | | Network::FilterStatus onData(Buffer::Instance&, bool) override; |
76 | | Network::FilterStatus onNewConnection() override; |
77 | | |
78 | 0 | void initializeReadFilterCallbacks(Network::ReadFilterCallbacks& read_callbacks) override { |
79 | 0 | read_callbacks_ = &read_callbacks; |
80 | 0 | read_callbacks_->connection().addConnectionCallbacks(*this); |
81 | 0 | } |
82 | | |
83 | | // Network::ConnectionCallbacks |
84 | | void onEvent(Network::ConnectionEvent event) override; |
85 | 0 | void onAboveWriteBufferHighWatermark() override {} |
86 | 0 | void onBelowWriteBufferLowWatermark() override {} |
87 | | |
88 | | private: |
89 | | void resetTimerState(); |
90 | | const ConfigSharedPtr config_; |
91 | | Network::ReadFilterCallbacks* read_callbacks_{}; |
92 | | Event::TimerPtr delay_timer_ = nullptr; |
93 | | bool is_rejected_{false}; |
94 | | }; |
95 | | |
96 | | } // namespace ConnectionLimitFilter |
97 | | } // namespace NetworkFilters |
98 | | } // namespace Extensions |
99 | | } // namespace Envoy |