forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection_handler_impl.h
250 lines (213 loc) · 9.47 KB
/
connection_handler_impl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#pragma once
#include <atomic>
#include <cstdint>
#include <list>
#include <memory>
#include "envoy/common/time.h"
#include "envoy/event/deferred_deletable.h"
#include "envoy/network/connection.h"
#include "envoy/network/connection_handler.h"
#include "envoy/network/filter.h"
#include "envoy/network/listen_socket.h"
#include "envoy/network/listener.h"
#include "envoy/server/active_udp_listener_config.h"
#include "envoy/server/listener_manager.h"
#include "envoy/stats/scope.h"
#include "envoy/stats/timespan.h"
#include "common/common/linked_object.h"
#include "common/common/non_copyable.h"
#include "spdlog/spdlog.h"
namespace Envoy {
namespace Server {
#define ALL_LISTENER_STATS(COUNTER, GAUGE, HISTOGRAM) \
COUNTER(downstream_cx_destroy) \
COUNTER(downstream_cx_total) \
COUNTER(downstream_pre_cx_timeout) \
COUNTER(no_filter_chain_match) \
GAUGE(downstream_cx_active, Accumulate) \
GAUGE(downstream_pre_cx_active, Accumulate) \
HISTOGRAM(downstream_cx_length_ms)
/**
* Wrapper struct for listener stats. @see stats_macros.h
*/
struct ListenerStats {
ALL_LISTENER_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT, GENERATE_HISTOGRAM_STRUCT)
};
#define ALL_PER_HANDLER_LISTENER_STATS(COUNTER, GAUGE) \
COUNTER(downstream_cx_total) \
GAUGE(downstream_cx_active, Accumulate)
/**
* Wrapper struct for per-handler listener stats. @see stats_macros.h
*/
struct PerHandlerListenerStats {
ALL_PER_HANDLER_LISTENER_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT)
};
/**
* Server side connection handler. This is used both by workers as well as the
* main thread for non-threaded listeners.
*/
class ConnectionHandlerImpl : public Network::ConnectionHandler,
NonCopyable,
Logger::Loggable<Logger::Id::conn_handler> {
public:
ConnectionHandlerImpl(Event::Dispatcher& dispatcher, const std::string& per_handler_stat_prefix);
// Network::ConnectionHandler
uint64_t numConnections() override { return num_connections_; }
void incNumConnections() override;
void decNumConnections() override;
void addListener(Network::ListenerConfig& config) override;
void removeListeners(uint64_t listener_tag) override;
void stopListeners(uint64_t listener_tag) override;
void stopListeners() override;
void disableListeners() override;
void enableListeners() override;
const std::string& statPrefix() override { return per_handler_stat_prefix_; }
Network::Listener* findListenerByAddress(const Network::Address::Instance& address) override;
Network::ConnectionHandler::ActiveListener*
findActiveListenerByAddress(const Network::Address::Instance& address);
/**
* Wrapper for an active listener owned by this handler.
*/
class ActiveListenerImplBase : public Network::ConnectionHandler::ActiveListener {
public:
ActiveListenerImplBase(Network::ConnectionHandler& parent, Network::ListenerPtr&& listener,
Network::ListenerConfig& config);
// Network::ConnectionHandler::ActiveListener.
uint64_t listenerTag() override { return listener_tag_; }
Network::Listener* listener() override { return listener_.get(); }
void destroy() override { listener_.reset(); }
Network::ListenerPtr listener_;
ListenerStats stats_;
PerHandlerListenerStats per_worker_stats_;
const std::chrono::milliseconds listener_filters_timeout_;
const bool continue_on_listener_filters_timeout_;
const uint64_t listener_tag_;
Network::ListenerConfig& config_;
};
private:
struct ActiveConnection;
using ActiveConnectionPtr = std::unique_ptr<ActiveConnection>;
struct ActiveSocket;
using ActiveSocketPtr = std::unique_ptr<ActiveSocket>;
/**
* Wrapper for an active tcp listener owned by this handler.
*/
class ActiveTcpListener : public Network::ListenerCallbacks, public ActiveListenerImplBase {
public:
ActiveTcpListener(ConnectionHandlerImpl& parent, Network::ListenerConfig& config);
ActiveTcpListener(ConnectionHandlerImpl& parent, Network::ListenerPtr&& listener,
Network::ListenerConfig& config);
~ActiveTcpListener() override;
// Network::ListenerCallbacks
void onAccept(Network::ConnectionSocketPtr&& socket,
bool hand_off_restored_destination_connections) override;
void onNewConnection(Network::ConnectionPtr&& new_connection) override;
/**
* Remove and destroy an active connection.
* @param connection supplies the connection to remove.
*/
void removeConnection(ActiveConnection& connection);
/**
* Create a new connection from a socket accepted by the listener.
*/
void newConnection(Network::ConnectionSocketPtr&& socket);
ConnectionHandlerImpl& parent_;
std::list<ActiveSocketPtr> sockets_;
std::list<ActiveConnectionPtr> connections_;
};
/**
* Wrapper for an active connection owned by this handler.
*/
struct ActiveConnection : LinkedObject<ActiveConnection>,
public Event::DeferredDeletable,
public Network::ConnectionCallbacks {
ActiveConnection(ActiveTcpListener& listener, Network::ConnectionPtr&& new_connection,
TimeSource& time_system);
~ActiveConnection() override;
// Network::ConnectionCallbacks
void onEvent(Network::ConnectionEvent event) override {
// Any event leads to destruction of the connection.
if (event == Network::ConnectionEvent::LocalClose ||
event == Network::ConnectionEvent::RemoteClose) {
listener_.removeConnection(*this);
}
}
void onAboveWriteBufferHighWatermark() override {}
void onBelowWriteBufferLowWatermark() override {}
ActiveTcpListener& listener_;
Network::ConnectionPtr connection_;
Stats::TimespanPtr conn_length_;
};
/**
* Wrapper for an active accepted socket owned by this handler.
*/
struct ActiveSocket : public Network::ListenerFilterManager,
public Network::ListenerFilterCallbacks,
LinkedObject<ActiveSocket>,
public Event::DeferredDeletable {
ActiveSocket(ActiveTcpListener& listener, Network::ConnectionSocketPtr&& socket,
bool hand_off_restored_destination_connections)
: listener_(listener), socket_(std::move(socket)),
hand_off_restored_destination_connections_(hand_off_restored_destination_connections),
iter_(accept_filters_.end()) {
listener_.stats_.downstream_pre_cx_active_.inc();
}
~ActiveSocket() override {
accept_filters_.clear();
listener_.stats_.downstream_pre_cx_active_.dec();
}
void onTimeout();
void startTimer();
void unlink();
void newConnection();
// Network::ListenerFilterManager
void addAcceptFilter(Network::ListenerFilterPtr&& filter) override {
accept_filters_.emplace_back(std::move(filter));
}
// Network::ListenerFilterCallbacks
Network::ConnectionSocket& socket() override { return *socket_.get(); }
Event::Dispatcher& dispatcher() override { return listener_.parent_.dispatcher_; }
void continueFilterChain(bool success) override;
ActiveTcpListener& listener_;
Network::ConnectionSocketPtr socket_;
const bool hand_off_restored_destination_connections_;
std::list<Network::ListenerFilterPtr> accept_filters_;
std::list<Network::ListenerFilterPtr>::iterator iter_;
Event::TimerPtr timer_;
};
Event::Dispatcher& dispatcher_;
const std::string per_handler_stat_prefix_;
std::list<std::pair<Network::Address::InstanceConstSharedPtr,
Network::ConnectionHandler::ActiveListenerPtr>>
listeners_;
std::atomic<uint64_t> num_connections_{};
bool disable_listeners_;
};
/**
* Wrapper for an active udp listener owned by this handler.
* TODO(danzh): rename to ActiveRawUdpListener.
*/
class ActiveUdpListener : public Network::UdpListenerCallbacks,
public ConnectionHandlerImpl::ActiveListenerImplBase,
public Network::UdpListenerFilterManager,
public Network::UdpReadFilterCallbacks {
public:
ActiveUdpListener(Network::ConnectionHandler& parent, Event::Dispatcher& dispatcher,
Network::ListenerConfig& config);
ActiveUdpListener(Network::ConnectionHandler& parent, Network::ListenerPtr&& listener,
Network::ListenerConfig& config);
// Network::UdpListenerCallbacks
void onData(Network::UdpRecvData& data) override;
void onWriteReady(const Network::Socket& socket) override;
void onReceiveError(const Network::UdpListenerCallbacks::ErrorCode& error_code,
Api::IoError::IoErrorCode err) override;
// Network::UdpListenerFilterManager
void addReadFilter(Network::UdpListenerReadFilterPtr&& filter) override;
// Network::UdpReadFilterCallbacks
Network::UdpListener& udpListener() override;
private:
Network::UdpListener& udp_listener_;
Network::UdpListenerReadFilterPtr read_filter_;
};
} // namespace Server
} // namespace Envoy